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:

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

Provided by Scrapfly

This knowledgebase is provided by Scrapfly data APIs, check us out! 👇