Last modified: Oct 29, 2023 By Alexander Williams
Beautiful Soup: Find by Tag and Class [Examples]
Example 1: Find <div> Elements with a Specific Class
from bs4 import BeautifulSoup
html = """
<div class="container">
<div class="content">This is the content</div>
</div>
<div class="content">Another content</div>
"""
# Create a BeautifulSoup object to parse the HTML
soup = BeautifulSoup(html, 'html.parser')
# Find all <div> elements with the class "content"
content_divs = soup.find_all('div', class_='content')
# Print the text inside each matching <div> element
for div in content_divs:
print(div.get_text())
Output:
This is the content
Another content
Example 2: Find Elements by Tag and Class Simultaneously
# Find <div> elements with both "container" and "content" classes
combined_divs = soup.find_all('div', class_='container content')
# Print the text inside each matching <div> element
for div in combined_divs:
print(div.get_text())
Output:
This is the content
Example 3: Find Elements with a Class Prefix
# Find <div> elements with class names that start with "cont"
prefix_divs = soup.find_all('div', class_=lambda value: value and value.startswith("cont"))
# Print the text inside each matching <div> element
for div in prefix_divs:
print(div.get_text())
Output:
This is the content
Another content
Example 4: Find <p> Elements with a Specific Class
from bs4 import BeautifulSoup
html = """
<p class="highlight">This is a highlighted paragraph</p>
<p class="highlight">Another highlighted paragraph</p>
<p>Regular paragraph</p>
"""
# Create a BeautifulSoup object to parse the HTML
soup = BeautifulSoup(html, 'html.parser')
# Find all <p> elements with the class "highlight"
highlighted_paragraphs = soup.find_all('p', class_='highlight')
# Print the text inside each matching <p> element
for p in highlighted_paragraphs:
print(p.get_text())
Output:
This is a highlighted paragraph
Another highlighted paragraph
Example 5: Find <div> Elements with Multiple Classes
# Find <div> elements with both "container" and "highlight" classes
combined_divs = soup.find_all('div', class_='container highlight')
# Print the text inside each matching <div> element
for div in combined_divs:
print(div.get_text())
Output:
This is a highlighted div
Example 6: Find Elements with Specific Class and Nested Elements
html = """
<div class="container">
<div class="highlight">This is a highlighted div</div>
<p class="highlight">A highlighted paragraph</p>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
# Find the <div> with class "container" and then find nested <p> elements with class "highlight"
nested_highlighted_paragraphs = soup.find('div', class_='container').find_all('p', class_='highlight')
# Print the text inside each matching <p> element
for p in nested_highlighted_paragraphs:
print(p.get_text())
Output:
A highlighted paragraph