我试图做以下事情:
搜索必须像www.site.com/1 www.site.com/2 www.site.com/3 ...等
HTML来源:
<pre class="values">
<strong>A</strong>
<strong>B</strong>
<strong>C</strong>
<span id="1">
<a href="/#">+</a>
<span title="1">1</span>
<a href="/#">XXX</a>
<a href="/#">YYY</a>
</span>
</pre>
文本文件高效0(1)使用集合查找:
with open("values.txt", "r") as f1:
lines = set(f1) # efficient 0(1) lookups using a set
for line in HTML :
if line in lines:
print(line)
答案 0 :(得分:1)
from xml.etree import ElementTree as ET
<pre class="values">
<strong>A</strong>
<strong>B</strong>
<strong>C</strong>
<span id="1">
<a href="/#">+</a>
<span title="1">1</span>
<a href="/#">XXX</a> <a href="/#">YYY</a>
</span>
</pre>
with open('/path/to/file.html') as fp:
html = ET.fromstring(fp.read())
for node in html.iter():
if node.tag == 'a':
print node.text