How to find HTML elements by text value with BeautifulSoup

by scrapecrow Oct 26, 2022

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:

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))

Related Articles

How to Scrape YouTube in 2025

Learn how to scrape YouTube, channel, video, and comment data using Python directly in JSON.

SCRAPEGUIDE
PYTHON
HIDDEN-API
How to Scrape YouTube in 2025

Bypass Proxy Detection with Browser Fingerprint Impersonation

Stop proxy blocks with browser fingerprint impersonation using this guide for Playwright, Selenium, curl-impersonate & Scrapfly

PROXIES
SELENIUM
PLAYWRIGHT
PUPPETEER
BLOCKING
Bypass Proxy Detection with Browser Fingerprint Impersonation

How Caching Can Cut Your Proxy Bill by 70%

Learn how intelligent caching strategies can reduce proxy costs by 40-70%. Complete guide to bandwidth optimization and proxy management.

PROXIES
How Caching Can Cut Your Proxy Bill by 70%

How to Optimize NetNut Proxies

Learn how to set up and optimize NetNut proxies for web scraping, including bandwidth reduction techniques and integration with Scrapfly Proxy Saver.

PROXIES
How to Optimize NetNut Proxies

How to Optimize Webshare Proxies

Webshare is a fast-growing proxy provider offering affordable proxy solutions for various web scraping and automation tasks. Here's how to make best of it.

PROXIES
How to Optimize Webshare Proxies

How to Optimize Oxylabs Proxies

Learn how to optimize Oxylabs proxies for efficient web scraping using Python and Scrapfly Proxy Saver. Reduce bandwidth, improve speed, and cut costs.

PROXIES
How to Optimize Oxylabs Proxies