Using CSS selectors in BeautifulSoup
suggest changeBeautiful 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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents