推荐学习书目
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
commoccoom
V2EX  ›  Python

又来提问了。

  •  
  •   commoccoom · Jun 18, 2015 · 3647 views
    This topic created in 4051 days ago, the information mentioned may be changed or developed.
    14 replies    2015-06-21 06:44:50 +08:00
    commoccoom
        1
    commoccoom  
    OP
       Jun 18, 2015
    为什么 list1 = re.findall(r'<a href="(.*?)" name="readlink" id="[\s\S]+" class="subject_t f12" >(.*?)</a>',html)

    html 是整个页面,为什么只抓取到了5个
    Septembers
        3
    Septembers  
       Jun 18, 2015 via Android   ❤️ 1
    你应该用 requests和BeautifulSoup 来处理这个问题
    而不是 urllib和正则 来处理这个问题
    josephok
        4
    josephok  
       Jun 18, 2015   ❤️ 1
    用xpath吧
    mhycy
        5
    mhycy  
       Jun 18, 2015   ❤️ 1
    <a href="(.*?)" name="readlink" id="(.*?)" class="subject_t f12" >(.*?)<\/a>
    改成这样试试。。
    imn1
        6
    imn1  
       Jun 18, 2015   ❤️ 1
    <a href="(.*?)" name="readlink" id="[^"]+" class="subject_t f12" >(.*?)</a>
    imn1
        7
    imn1  
       Jun 18, 2015   ❤️ 1
    养成习惯,引号内都用 [^"]+ 或 [^']+
    或者这样说:左右边界清晰的内容,泛匹配的话,使用排除边界来匹配

    能用正则尽量雍正则,bs / xpath都需要一个额外的dom解析过程,大量页面的话资源消耗很严重
    commoccoom
        8
    commoccoom  
    OP
       Jun 18, 2015
    @imn1

    非常感谢!
    Septembers
        9
    Septembers  
       Jun 18, 2015 via Android
    @imn1 大量文本提取信息格式稳定的话 正则是要好得多 但是前提是熟悉正则(我以前写正则一不注意没写好 就进贪婪匹配 然后就over了 唉

    如果在不熟悉正则的情况下处理HTML
    还是老老实实的DOM解析比较好
    garfeildma
        10
    garfeildma  
       Jun 18, 2015
    简单的一次性的工作可以用正则来完成,正规做法还是老老实实解析html,原因看这里
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags
    loading
        11
    loading  
       Jun 18, 2015 via Android   ❤️ 1
    下次标题写明白点…求您了!
    Axurez
        12
    Axurez  
       Jun 18, 2015
    正则开销会比较大吧,复杂一点的基本不可能实际运行的。。
    commoccoom
        13
    commoccoom  
    OP
       Jun 18, 2015
    @loading

    不好意思。。。。。我不知道用什么标题合适
    fltw
        14
    fltw  
       Jun 21, 2015
    不知道楼主在说什么啊,我这连连接都打不开。。。
    正则表达式太。。。。。怎么说,必须要熟练,否则很痛苦的,需要经常修改
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5674 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 81ms · UTC 07:13 · PVG 15:13 · LAX 00:13 · JFK 03:13
    ♥ Do have faith in what you're doing.