Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lxml - get a flat list of elements

I'd like to flatten an lxml etree (specifically, HTML, if it matters.) How would I go about getting a flat list of all elements in the tree?

like image 511
Walrus the Cat Avatar asked Oct 06 '14 19:10

Walrus the Cat


1 Answers

You can use the .iter() method, like so:

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]
like image 173
Robᵩ Avatar answered Sep 20 '22 13:09

Robᵩ