【发布时间】:2014-10-22 15:19:09
【问题描述】:
我在一个 HTML 页面中有一些这样的行:
<div>
<p class="match"> this sentence should match </p>
some text
<a class="a"> some text </a>
</div>
<div>
<p class="match"> this sentence shouldnt match</p>
some text
<a class ="b"> some text </a>
</div>
我想提取<p class="match"> 内的行,但前提是div 内包含<a class="a">。
到目前为止我所做的如下(我首先找到带有<a class="a"> 的段落,然后迭代结果以在<p class="match"> 中找到句子):
import re
file_to_r = open("a")
regex_div = re.compile(r'<div>.+"a".+?</div>', re.DOTALL)
regex_match = re.compile(r'<p class="match">(.+)</p>')
for m in regex_div.findall(file_to_r.read()):
print(regex_match.findall(m))
但我想知道是否有其他(仍然有效的)方法可以一次完成?
【问题讨论】:
-
尝试漂亮的汤4解析html文件..
标签: python html regex html-parsing