🚀 We are hiring! See open positions

How to find HTML elements by text value with BeautifulSoup

by Bernardas Alisauskas May 29, 2023 1 min read

Using Python and Beautifulsoup we can find any HTML element by partial or exact text value using find / find_all method by passing regular expressions object to the text parameter:

python
import re
import bs4

soup = bs4.BeautifulSoup('<a>Twitter link</a>')

# case sensitive:
soup.find("a", text=re.compile("Twitter"))  # will find 1st ocurrance 
soup.find_all("a", text=re.compile("Twitter"))  # will find all ocurrances
# case insensitive:
soup.find("a", text=re.compile("twitter", re.I))
soup.find_all("a", text=re.compile("twitter", re.I))
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.