Using CSS selectors in BeautifulSoup

suggest change

Beautiful Soup is the third-party beautifulsoup4 package. select() finds multiple elements and select_one() finds one. Since Beautiful Soup 4.7 those methods use Soup Sieve, which covers far more than the older matcher. Before 4.7, select() only supported the most common CSS selectors.

Basic example:

from bs4 import BeautifulSoup

data = """
<ul>
    <li class="item">item1</li>
    <li class="item">item2</li>
    <li class="item">item3</li>
</ul>
"""

soup = BeautifulSoup(data, "html.parser")

for item in soup.select("li.item"):
    print(item.get_text())

Prints:

item1
item2
item3

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents