我想展平一个 lxml etree(特别是 HTML,如果重要的话。)我将如何获得树中所有元素的平面列表?
最佳答案
您可以使用 .iter()
方法,像这样:
from lxml import etree
xml = etree.XML('''<html><body>
<p>hi there</p><p>2nd paragraph</p>
</body></html>''')
# If you want to visit all of the descendants
for element in xml.iter():
print element.tag
# Or, if you want to have a list of all the descendents
all_elements = list(xml.iter())
print [element.tag for element in all_elements]
关于python - lxml - 获取元素的平面列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26223693/