Searching the DOM with Beautiful Soup's find methods
We can perform simple searches of the DOM using Beautiful Soup's find methods. These methods give us a much more flexible and powerful construct for finding elements that are not dependent upon the hierarchy of those elements. In this recipe we will examine several common uses of these functions to locate various elements in the DOM.
Getting ready
ff you want to cut and paste the following into ipython, you can find the samples in 02/02_bs4_find.py
.
How to do it...
We will start with a fresh iPython session and start by loading the planets page:
In [1]: import requests ...: from bs4 import BeautifulSoup ...: html = requests.get("http://localhost:8080/planets.html").text ...: soup = BeautifulSoup(html, "lxml") ...:
In the previous recipe, to access all of the <tr>
in the table, we used a chained property syntax to get the table, and then needed to get the children and iterator over them. This does have a problem as the children could...