LXML and Family Trees
lxml
also has the ability to traverse family trees within the HTML page. What is a family tree? When you used your browser's developer tools to investigate the elements on the page and you were able to expand or retract them, you were observing family relationships in the HTML. Every element on a web page can have parents, siblings and children. These relationships can help us more easily traverse the page.
For example, if I want to find all the elements at the same node depth level on the page, I would be looking for their siblings. Or maybe I want every element that is a child of a particular element on the page. lxml
allows us to use many of these relationships with simple Python code.
As an example, let's investigate all children of the table
element on the example page:
>>> table = tree.xpath('//table')[0] >>> table.getchildren() [<Element tr at 0x7f525158ec78>, <Element tr at 0x7f52515ad638>, <Element tr at 0x7f52515ad5e8>,...