推荐学习书目
› 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 · Feb 13, 2017 · 5231 views
    This topic created in 3554 days ago, the information mentioned may be changed or developed.

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

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

    1L 和 3L 并非错误,但是不是标准做法。
    2L 其实给出了相对更好的答案,解析网页还是上 BeautifulSoup 更好。
    ipwx
    ipwx
    Feb 13, 2017❤️1
    5
    好吧,审题不清。 1L 和 3L 的 .*? 还是必要的。加上 re.S 你就不用 (.|\n)*? 了。
    Allianzcortex
    Allianzcortex
    Feb 13, 2017 via iPhone
    6
    .*+ 后面加一个 ? 表示匹配最少的结果~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1840 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 03:55 · PVG 11:55 · LAX 19:55 · JFK 22:55
    ♥ Do have faith in what you're doing.