Traversing the tree in Python
Walking nested tags to extract a table's rows.
from bs4 import BeautifulSoup
html = """
<table>
<tr><th>lang</th><th>year</th></tr>
<tr><td>Python</td><td>1991</td></tr>
<tr><td>Rust</td><td>2010</td></tr>
</table>
"""
soup = BeautifulSoup(html, "html.parser")
rows = []
for tr in soup.find("table").find_all("tr")[1:]:
cells = [td.get_text(strip=True) for td in tr.find_all("td")]
rows.append(cells)
first_lang = rows[0][0]
How it works
find_all('tr')[1:]skips the header row.- Each row's cells are read with
get_text. - The result is a list of row lists.
Keywords and builtins used here
for
The run, in numbers
- Lines
- 17
- Characters to type
- 380
- Tokens
- 88
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 48 seconds.
Step 3 of 3 in BeautifulSoup, step 8 of 9 in Web scraping.