V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2015-06-18 08:51:01 +08:00 · 3058 次点击
    这是一个创建于 3229 天前的主题,其中的信息可能已经有所发展或是发生改变。
    14 条回复    2015-06-21 06:44:50 +08:00
    commoccoom
        1
    commoccoom  
    OP
       2015-06-18 08:52:52 +08:00
    为什么 list1 = re.findall(r'<a href="(.*?)" name="readlink" id="[\s\S]+" class="subject_t f12" >(.*?)</a>',html)

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

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

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

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

    不好意思。。。。。我不知道用什么标题合适
    fltw
        14
    fltw  
       2015-06-21 06:44:50 +08:00
    不知道楼主在说什么啊,我这连连接都打不开。。。
    正则表达式太。。。。。怎么说,必须要熟练,否则很痛苦的,需要经常修改
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2362 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:08 · PVG 00:08 · LAX 09:08 · JFK 12:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.