推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
haomni
V2EX  ›  Python

Python 提取 POST 返回的 Response

  •  
  •   haomni · Apr 10, 2016 · 11048 views
    This topic created in 3860 days ago, the information mentioned may be changed or developed.

    看到有人在 http://v2ex.com/t/269567 求助,准备在 @hzlzh 脚本基础上增加一个 exurl

    自己刚开始学 Python 不久,能力不足,目前卡在从下面 POST 返回的代码中提取 http://test.long.url/ 这个返回的链接,其余都差不多了

    <table id="response" border="0" cellpadding="0" cellspacing="0">
    <tr><td class="shorturl">http://test/shorturl</td><td class="longurl"><a href="http://test.long.url/" target="_blank">http://test.long.url/</a></td></tr>	</table>
    

    看了下面两个帖子弄了半天还是没搞定,求助各位

    先谢谢各位~

    21 replies  •  2016-04-10 22:22:19 +08:00
    eoo
    eoo
    Apr 10, 2016 via Android
    1
    要 POST 的地址呢?
    haomni
    haomni
    OP
    Apr 10, 2016
    2
    @eoo 感谢回复,从 POST 返回的结果中抓取 URL 应该不需要原来的 POST 地址吧……
    virusdefender
    virusdefender
    Apr 10, 2016
    3
    # coding=utf-8
    import re

    html = """
    <table id="response" border="0" cellpadding="0" cellspacing="0">
    <tr><td class="shorturl">http://test/shorturl</td><td class="longurl"><a href="http://test.long.url/"
    """

    print re.compile('<td class="shorturl">([\s\S]*?)</td>').findall(html)[0]
    haomni
    haomni
    OP
    Apr 10, 2016
    4
    @virusdefender 感谢,但是这个找出来的是 shorturl
    我换成 longurl 之后结果是:
    <a href="http://test.long.url/" target="_blank">http://test.long.url/</a>

    还是没达成目的……
    uyhyygyug1234
    uyhyygyug1234
    Apr 10, 2016
    5
    # coding=utf-8
    import re


    html = """
    <table id="response" border="0" cellpadding="0" cellspacing="0">
    <tr><td class="shorturl">http://test/shorturl</td><td class="longurl"><a href="http://test.long.url/"
    """

    print re.compile('href="(.*)"').findall(html)[0]
    uyhyygyug1234
    uyhyygyug1234
    Apr 10, 2016
    6
    这样可以不过是在太丑了。应该上 bs4 , pyquery 之类的额
    haomni
    haomni
    OP
    Apr 10, 2016
    7
    @uyhyygyug1234 大侠 结果好像不太对啊 也可能是我 Reponse 结果没有贴全的缘故

    >>> print re.compile('href="(.*)"').findall(req.content)[0]
    /screen.css

    class="longurl" 这个在整个 Response 中是唯一的,现在要的是取后面那个指向链接
    sh4n3
    sh4n3
    Apr 10, 2016
    8
    用 .longurl a 这样的 css Selector 就好了。
    ericls
    ericls
    Apr 10, 2016
    9
    直接 pyquery 来搞
    eoo
    eoo
    Apr 10, 2016
    10
    @haomni 用 PHP 很容易

    <?php

    $str='<table id="response" border="0" cellpadding="0" cellspacing="0">
    <tr><td class="shorturl">http://test/shorturl</td><td class="longurl"><a href="http://test.long.url/" target="_blank">http://test.long.url/</a></td></tr> </table>';

    $zz='#<td class="longurl"><a href="(.*?)" target="_blank">.*?</a></td>#';

    preg_match($zz, $str, $matchs);

    print_r($matchs);
    haomni
    haomni
    OP
    Apr 10, 2016
    11
    @uyhyygyug1234
    @ericls
    试了下 PyQuery ,可能我用法不太对
    print doc1('class:contains("longurl")')

    @eoo 不准备再换 php 了,其它都写好了
    eoo
    eoo
    Apr 10, 2016
    12
    @haomni 好吧 ,写的什么?
    seki
    seki
    Apr 10, 2016
    13
    为啥不用 beautifulsoup 或者 lxml
    seki
    seki
    Apr 10, 2016
    14
    嗯比方说你的 bs4 提取失败的代码是什么样的
    haomni
    haomni
    OP
    Apr 10, 2016
    15
    感谢各位, PyQuery 不太会用
    在 @uyhyygyug1234 的基础上再用一次正则就搞定了

    @seki 唉,虽然有心想用,但是不会啊……
    longchisihai
    longchisihai
    Apr 10, 2016
    16
    from bs4 import BeautifulSoup

    html = '''<table id="response" border="0" cellpadding="0" cellspacing="0">
    <tr><td class="shorturl">http://test/shorturl</td><td class="longurl"><a href="http://test.long.url/" target="_blank">http://test.long.url/</a></td></tr> </table>'''

    soup = BeautifulSoup(html, 'lxml')

    longurl_tag = soup.find('td', class_ = 'longurl')

    print (longurl_tag.contents[0].get('href'))
    haomni
    haomni
    OP
    Apr 10, 2016
    17
    @longchisihai 简直完美,感谢!
    haomni
    haomni
    OP
    Apr 10, 2016
    18
    大致的样子有了,
    弄了一宿,先去睡一会,醒了再测,先上个图压压惊
    再次感谢各位技术帝帮忙,稍后会将作品上传到 Github 开源

    hzlzh
    hzlzh
    Apr 10, 2016
    19
    干得漂亮~
    haomni
    haomni
    OP
    Apr 10, 2016
    20
    @hzlzh 都是在前辈的基础上改的 还有一些细节没有完善
    弄好了我再联系你~
    davidx
    davidx
    Apr 10, 2016
    21
    这个时候 你们需要 https://daimaduan.com
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1755 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 82ms · UTC 17:19 · PVG 01:19 · LAX 09:19 · JFK 12:19
    ♥ Do have faith in what you're doing.