>>> root = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")
#Find a child of an Element:
>>> print(root.find("b"))
None
>>> print(root.find("a").tag)
a
#Find an Element anywhere in the tree:
>>> print(root.find(".//b").tag)
b
>>> [ b.tag for b in root.iterfind(".//b") ]
['b', 'b']
#Find Elements with a certain attribute:
>>> print(root.findall(".//a[@x]")[0].tag)
a
>>> print(root.findall(".//a[@y]"))
[]
iterfind()
遍历与路径表达式匹配的所有元素findall()
返回匹配元素的列表find()
有效地只返回第一个匹配项findtext()
返回第一个匹配项的.text内容示例:
参考:http://lxml.de/tutorial.html#elementpath
(此答案是从该链接的内容中进行的相关选择性选择)
可以使用XPath递归搜索:
相关问题 更多 >
编程相关推荐