How to find HTML elements by text value with BeautifulSoup

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))
Question tagged: