typestar

CSS selectors in Python

Finding elements with familiar CSS selector syntax.

from bs4 import BeautifulSoup

html = """
<ul class="menu">
  <li class="item"><a href="/py">Python</a></li>
  <li class="item featured"><a href="/rs">Rust</a></li>
  <li class="item"><a href="/go">Go</a></li>
</ul>
"""

soup = BeautifulSoup(html, "html.parser")
items = soup.select("ul.menu li.item")
featured = soup.select_one("li.featured a").text
hrefs = [a["href"] for a in soup.select("li a")]
count = len(items)

How it works

  1. select returns every match for a selector.
  2. select_one returns just the first.
  3. A list comprehension pulls each href.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
412
Tokens
100
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 57 seconds.

Type this snippet

Step 2 of 3 in BeautifulSoup, step 7 of 9 in Web scraping.

← Previous Next →