Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Web Scraping

You're reading from   Python Web Scraping Hands-on data scraping and crawling using PyQT, Selnium, HTML and Python

Arrow left icon
Product type Paperback
Published in May 2017
Publisher
ISBN-13 9781786462589
Length 220 pages
Edition 2nd Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Katharine Jarmul Katharine Jarmul
Author Profile Icon Katharine Jarmul
Katharine Jarmul
Arrow right icon
View More author details
Toc

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime