Querying data with XPath and CSS selectors

CSS selectors are patterns used for selecting elements and are often used to define the elements that styles should be applied to. They can also be used with lxml to select nodes in the DOM. CSS selectors are commonly used as they are more compact than XPath and generally can be more reusable in code. Examples of common selectors which may be used are as follows:

What you are looking for Example
All tags *
A specific tag (that is, tr) .planet
A class name (that is, "planet") tr.planet
A tag with an ID "planet3" tr#planet3
A child tr of a table table tr
A descendant tr of a table table tr
A tag with an attribute (that is, tr with id="planet4") a[id=Mars]

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.