【发布时间】:2012-12-19 23:12:56
【问题描述】:
我正在寻找 HTML 页面中的重复模式。
我感兴趣的模式在前缀“
Seasons
”之后开始同样的模式也出现在前缀之前,我对那些不感兴趣。
我尝试使用以下 python 代码(但失败了)(我将模式简化为 '' 为了使这个问题易于阅读):
matches = re.compile('<h2>Seasons</h2>.+?(<a href=.+?</a>)+',re.DOTALL).findall(page)
for ref in matches
print ref
给定页面:
blah blah html stuff
<h2>Seasons</h2>
blah blah more html stuff
<a href=http://www.111.com>111</a><a href=http://www.222.com>222</a><a href=http://www.333.com>333</a>
输出是
<a href=http://www.333.com>333</a>
所以它只打印最后一个匹配,其他两个不进入 findall 列表。 如何遍历组的所有匹配项?
【问题讨论】:
-
当有许多出色的 HTML 解析器可以更好地完成这项工作并使其更容易时,您为什么还要尝试使用正则表达式来解析 HTML?