【发布时间】:2020-09-11 14:32:52
【问题描述】:
我有一个 html 文件,我试图在其中搜索一个模式,当我找到它时,打印设置模式之间的所有行。在我的情况下,它是“pre 和 /pre”之间的所有内容,其中包含一个词干循环序列。我的目标是对大量的 html 文件进行排序并提取这些结构并将它们放在一个列表中,该列表可以像在 html 文件中出现的那样打印出来。我无法正确捕获所有线条并正确打印它们,因为它会出现在图片中。目前我只打印了两行,pre cugcaggcagaaguggggcugaca /pre 和 pre ccucaccccccuucugccugca /pre
为什么它没有捕获图案中的所有内容并正确打印?它正在跳过跨度和跨度类。我不确定我是否正确解决了这个问题——有没有更好的方法来考虑这个问题?谢谢
fh_html = open("filename").readlines()
for line in fh_html:
match_obj = re.search(r'<pre>.*</pre>', line, re.DOTALL)
print(match_obj.group(0))
<pre>ggggc <span class="sld">u</span> - <span class="sld">c</span> <span class="sld">ca</span> agag
<span class="sld">cugcaggcagaag</span> <span class="sld">ggg</span> <span class="sld">g</span> <span class="sld">uga</span> gggc g
||||||||||||| ||| | ||| ||||
g<span class="sld">acguccgucuuc</span> <span class="sld">ccc</span> <span class="sld">c</span> <span class="sld">acu</span> cccg g
----- - <span class="sld">a</span> <span class="sld">c</span> <span class="sld">cc</span> cguu </pre>
这是代码:
【问题讨论】:
标签: python regex html-parsing