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
abcdefghi
V2EX  ›  Python

请教老司机 findall 匹配多次遇到换行出现问题获取不到

  •  
  •   abcdefghi · 2017-02-13 01:38:05 +08:00 · 3942 次点击
    这是一个创建于 2631 天前的主题,其中的信息可能已经有所发展或是发生改变。

    html 有段循环列表

    <tr>
    <td class="xinbz" height="30"><input value="856252" name="xzid" type="checkbox"> <a href="856252" title="域名:medytL.com" target="_blank">aaa.com</a></td>
    <td>6</td>
    <td align="left" ><font color='#0000ff'>企业</font></td>
    <td>
    企业</td>
    <td align="center" ><a href="/?/130000" target='_blank' title="查看此卖家域名商铺">ID:130000</a></td>
    <td align="left" >2017-12-2</td>
    <td>111元</td>
    
    <td align="center"><a href="856252" target="_blank" title="进入域名: medytL.com ,购买页面"><img src="xbt.jpg" ></a></td>
    </tr>
    
    
    <tr>
    <td class="xinbz" height="30"><input value="856252" name="xzid" type="checkbox"> <a href="856252" title="域名:medytL.com" target="_blank">bbb.com</a></td>
    <td>6</td>
    <td align="left" ><font color='#0000ff'>企业</font></td>
    <td>
    企业</td>
    <td align="center" ><a href="/?/130000" target='_blank' title="查看此卖家域名商铺">ID:130000</a></td>
    <td align="left" >2017-12-2</td>
    <td>222元</td>
    
    <td align="center"><a href="856252" target="_blank" title="进入域名: medytL.com ,购买页面"><img src="xbt.jpg" ></a></td>
    </tr>
    
    

    我这段代码只能获取到第一组数据

    get_datas = re.findall(r'target="_blank">(.*)<\/a>(.|\n)*<td>(\d+)元</td>', html, re.M);
    print get_datas;
    

    [('uuupk.com', '\n', '111')]

    我想获取当前页面所有匹配的,类似 [('aaa.com', '\n', '111'),('bbb.com', '\n', '222')]

    findall 加了 re.M 还是获取不到多次,代码问题出现在了哪里呢? 先谢谢了

    6 条回复    2017-02-13 19:59:26 +08:00
    sola97
        1
    sola97  
       2017-02-13 02:47:39 +08:00   ❤️ 1
    你这么写是贪婪匹配,中间你得加问号 (.|\n)*?
    wddoer
        2
    wddoer  
       2017-02-13 09:33:27 +08:00   ❤️ 1
    为何不用 beautifulsoup 呢
    IanPeverell
        3
    IanPeverell  
       2017-02-13 09:41:12 +08:00   ❤️ 1
    1L 正解,因为匹配的时候会有两种选择('aaa.com', '\n', '111'),和('aaa.com', '\n', '222'),因为第一种在贪婪模式下优先级比较低,所以会直接处第二个结果,('bbb.com', '\n', '222')就被忽略掉了
    ipwx
        4
    ipwx  
       2017-02-13 12:48:23 +08:00   ❤️ 1
    把 re.M 改成 re.S

    re.M == re.MULTILINE 含义是 ^ 和 $ 匹配每一行的开头结尾,而不是整个字符串开头结尾。
    re.S == re.DOTALL 表示 . 能够匹配 \n

    1L 和 3L 并非错误,但是不是标准做法。
    2L 其实给出了相对更好的答案,解析网页还是上 BeautifulSoup 更好。
    ipwx
        5
    ipwx  
       2017-02-13 12:50:20 +08:00   ❤️ 1
    好吧,审题不清。 1L 和 3L 的 .*? 还是必要的。加上 re.S 你就不用 (.|\n)*? 了。
    Allianzcortex
        6
    Allianzcortex  
       2017-02-13 19:59:26 +08:00 via iPhone
    .*+ 后面加一个 ? 表示匹配最少的结果~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1005 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:51 · PVG 02:51 · LAX 11:51 · JFK 14:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.