How to do it...

The first thing that we do is to load the HTML into an lxml "etree".  This is lxml's representation of the DOM.

in [2]: tree = html.fromstring(page_html)

The tree variable is now an lxml representation of the DOM which models the HTML content. Let's now examine how to use it and XPath to select various elements from the document.

Out first XPath example will be to find all the the <tr> elements below the <table> element.

In [3]: [tr for tr in tree.xpath("/html/body/div/table/tr")]Out[3]:[<Element tr at 0x10cfd1408>, <Element tr at 0x10cfd12c8>, <Element tr at 0x10cfd1728>, <Element tr at 0x10cfd16d8>, <Element tr at 0x10cfd1458>, <Element tr at 0x10cfd1868>, <Element tr at 0x10cfd1318>, <Element tr at 0x10cfd14a8>, <Element ...

Get Python Web Scraping Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.