🚀 We are hiring! See open positions

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 Facebook: Marketplace and Events

Complete guide to scraping Facebook data including Marketplace listings and Events. Covers authentication, anti-bot bypass, and production-ready techniques.

PYTHON
PLAYWRIGHT
SCRAPEGUIDE
How to Scrape Facebook: Marketplace and Events

Crawl4AI Explained: The AI-Friendly Web Crawling Framework

Discover Crawl4AI, the AI-friendly web crawling framework. Learn features, installation, and intelligent web scraping for LLMs.

WEB-SCRAPING
AI
PYTHON
CRAWLING
Crawl4AI Explained: The AI-Friendly Web Crawling Framework

Web Scraping Services Explained

Explore web scraping services from managed APIs to self-hosted solutions. Learn use cases, pricing models, and how to choose the right data extraction service.

WEB-SCRAPING
TOOLS
Web Scraping Services Explained

Social Media Scraping in 2025

Complete guide to scraping Instagram, Twitter, TikTok, and LinkedIn with Python. Learn anti-blocking techniques for 2025.

SCRAPEGUIDE
PYTHON
SOCIAL-MEDIA
Social Media Scraping in 2025

Top Web Crawler Tools in 2025

Discover the best web crawler tools in 2025 for automated data collection. Compare features, performance, and use cases of Octoparse, Scrapy, and more.

TOOLS
WEB-SCRAPING
Top Web Crawler Tools in 2025

Top LangChain Alternatives in 2025

Explore the best LangChain alternatives in 2025 for building powerful AI applications. Compare features, performance, and use cases to find the right framework for your needs.

AI
LLM
LANGCHAIN
Top LangChain Alternatives in 2025