查找具有特定属性值的所有标签
问题描述:
如何遍历具有特定属性值的特定值的所有标签?例如,假设我们只需要data1,data2等。查找具有特定属性值的所有标签
<html>
<body>
<invalid html here/>
<dont care> ... </dont care>
<invalid html here too/>
<interesting attrib1="naah, it is not this"> ... </interesting tag>
<interesting attrib1="yes, this is what we want">
<group>
<line>
data
</line>
</group>
<group>
<line>
data1
<line>
</group>
<group>
<line>
data2
<line>
</group>
</interesting>
</body>
</html>
我试过BeautifulSoup但它无法解析文件。 LXML的解析器,似乎工作:
broken_html = get_sanitized_data(SITE)
parser = etree.HTMLParser()
tree = etree.parse(StringIO(broken_html), parser)
result = etree.tostring(tree.getroot(), pretty_print=True, method="html")
print(result)
我不熟悉它的API,我无法弄清楚如何为使用getiterator或XPath。
答
这里有一种方法,使用lxml和XPath'descendant::*[@attrib1="yes, this is what we want"]'
。 XPath通知lxml查看当前节点的所有后代,并返回attrib1
属性等于"yes, this is what we want"
的属性。
import lxml.html as lh
import cStringIO
content='''
<html>
<body>
<invalid html here/>
<dont care> ... </dont care>
<invalid html here too/>
<interesting attrib1="naah, it is not this"> ... </interesting tag>
<interesting attrib1="yes, this is what we want">
<group>
<line>
data
</line>
</group>
<group>
<line>
data1
<line>
</group>
<group>
<line>
data2
<line>
</group>
</interesting>
</body>
</html>
'''
doc=lh.parse(cStringIO.StringIO(content))
tags=doc.xpath('descendant::*[@attrib1="yes, this is what we want"]')
print(tags)
# [<Element interesting at b767e14c>]
for tag in tags:
print(lh.tostring(tag))
# <interesting attrib1="yes, this is what we want"><group><line>
# data
# </line></group><group><line>
# data1
# <line></line></line></group><group><line>
# data2
# <line></line></line></group></interesting>
+0
谢谢,你救了我的一天! – 2010-09-23 16:14:23
您是否尝试将MIME类型更改为XML?有些解析器很挑剔... – JKirchartz 2010-09-23 12:57:11
使用xpath的lxml似乎很容易,给文档一个机会:) http://codespeak.net/lxml/xpathxslt.html – 2010-09-23 13:04:57