Skip to content
Snell Rojas

Programming

BeautifulSoup Tips

Two BeautifulSoup tricks: find elements that match any of several classes, and find elements using a regex on their classes.

Snell RojasPublished Updated 1 min read

I wrote this post in Spanish in 2022 and adapted it to English in 2026.

Find elements by several classes:

Python
soup.findAll(True, {'class':['class1', 'class2']})

# example:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<html><body><div class="class1"></div><div class="class2"></div><div class="class3"></div></body></html>')
>>> soup.findAll(True, {"class":["class1", "class2"]})
[<div class="class1"></div>, <div class="class2"></div>]

Find elements using a regex for the classes:

Python
soup.find_all("td", class_=re.compile("cell_ab team-home | cell_ab team-home  bold"))