You can find a tag by specific text in BeautifulSoup using several methods. Here are the most common approaches:
1. Using string parameter
python
from bs4 import BeautifulSoup
html = """
<div>
<p>Hello World</p>
<p>Goodbye World</p>
<span>Hello World</span>
<a href="#">Click here</a>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
# Find tag with exact text
result = soup.find(string="Hello World")
print(result.parent) # <p>Hello World</p>
2. Using text parameter (deprecated in newer versions)
python
# For older versions of BeautifulSoup
result = soup.find(text="Hello World")
print(result.parent)
3. Using string with regular expressions
python
import re
# Find tags containing text that matches a pattern
result = soup.find(string=re.compile("World"))
print(result.parent) # <p>Hello World</p>
# Find all matches
results = soup.find_all(string=re.compile("World"))
for text in results:
print(f"Found: '{text}' in {text.parent.name}")
4. Using lambda function for complex conditions
python
# Find tags with text containing specific substring
result = soup.find(lambda tag: tag.string and "Hello" in tag.string)
print(result) # <p>Hello World</p>
# Find tags with text starting with specific text
result = soup.find(lambda tag: tag.string and tag.string.startswith("Hello"))
print(result)
# Find tags with text ending with specific text
result = soup.find(lambda tag: tag.string and tag.string.endswith("World"))
print(result)
5. Case-insensitive search
python
# Case-insensitive search
result = soup.find(lambda tag: tag.string and "hello" in tag.string.lower())
print(result)
6. Find all tags with specific text
python
# Find all tags containing the text
results = soup.find_all(string="Hello World")
for text in results:
print(text.parent) # <p>Hello World</p> and <span>Hello World</span>
# Or using lambda
results = soup.find_all(lambda tag: tag.string and "World" in tag.string)
for tag in results:
print(tag)
7. Advanced filtering with multiple conditions
python
# Find tags with specific text and specific tag type
result = soup.find('p', string="Hello World")
print(result) # <p>Hello World</p>
# Find tags with text containing specific text and specific class
result = soup.find('p', string=re.compile("Hello"))
print(result)
# Multiple conditions with lambda
result = soup.find(lambda tag: tag.name == 'p' and
tag.string and
"Hello" in tag.string)
print(result)
8. Practical example with real-world scenario
python
# Example: Find a button with specific text
html = """
<button class="submit-btn">Submit Form</button>
<button class="cancel-btn">Cancel</button>
<a href="#" class="link">Submit Form</a>
"""
soup = BeautifulSoup(html, 'html.parser')
# Find button with "Submit Form" text
submit_button = soup.find('button', string="Submit Form")
if submit_button:
print(f"Found submit button: {submit_button}")
# Find any element with "Cancel" text
cancel_element = soup.find(lambda tag: tag.string and "Cancel" in tag.string)
print(f"Cancel element: {cancel_element}")
Key Points:
Use string parameter for exact text matching
Use re.compile() for pattern matching
Use lambda functions for complex conditions
The text parameter is deprecated in newer versions
string returns the text node, so use .parent to get the containing tag
Always check if result exists before using it to avoid AttributeError
The lambda approach is the most flexible and recommended for complex text searching scenarios.