Lxml - get a flat list of elements

I would like to flatten the lxml etree (specifically HTML, if that matters.) How can I get a flat list of all the elements in the tree?

+3


source to share


1 answer


You can use a method .iter()

like:



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]

      

+6


source







All Articles