🚀 We are hiring! See open positions

How to find HTML element by class with BeautifulSoup?

Using Python and BeautifulSoup, to find elements by class name we can either use find and find_all functions with the class_ parameter or CSS selectors:

python
import bs4
soup = bs4.BeautifulSoup('<a class="social-link">some link</a>')

# using find() and find_all() methods:
soup.find("a", class_="social-link")  # alternatively find_all can be used to find all
soup.find("a", {"class": "social-link"})
# to find by partial class name we can use regex:
import re
soup.find("a", class_=re.compile("link", re.I))  # tip: re.I parameter makes this case insensitive

# using css selectors via select() and select_one() methods
soup.select('.social-link')
# to find by partial class name we can use `*=` matcher:
soup.select('[class*="link"]') 
# or
soup.select('[class*="link" i]')  # "i" addition makes this case insensitive
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.