Python BeautifulSoup  - Get HTML Elements by Class name


Python BeautifulSoup  - Get HTML Elements by Class name

You can get an HTML elements which have a specific class name using Python BeautifulSoup.

To get HTML elements by class name in Python using BeautifulSoup, parse the HTML content, and then use BeautifulSoup.find_all() method with class_ named argument.

Steps to get HTML elements by class name using BeautifulSoup

  1. Given HTML content in html_content as a string value.
  2. Parse the HTML content. Call BeautifulSoup() constructor, and pass the HTML content string as argument. The constructor returns a BeautifulSoup object.
soup = BeautifulSoup(html_content, 'html.parser')
  1. Call find_all() method on the returned BeautifulSoup object soup, and pass the class_ named argument. Specify the class name of required HTML elements, as value for the class_ named argument. The find_all() method returns a list of HTML elements with the specified class name attribute. If no element is found for the given class, then an empty list is returned.
elements_with_class = soup.find_all(class_="my_div")
  1. You may iterate over the returned list of HTML elements using a For loop.

Example to get HTML elements by Class name using BeautifulSoup

1. Get HTML elements whose class name is "my_div"

In the following program, we take a sample HTML content in html_content variable, and then find the HTML elements containing "my_div" as class name, using the above mentioned steps with BeautifulSoup.

Python Program

from bs4 import BeautifulSoup

# Sample HTML content
html_content = """
<html>
    <body>
        <div>This is div 1.</div>
        <div class="my_div">This is div 2.</div>
        <div class="my_div">This is div 3.</div>
        <div class="my_div">This is div 4.</div>
    </body>
</html>
"""

# Parse the HTML content
soup = BeautifulSoup(html_content, 'html.parser')

# Get elements by class name
elements_with_class = soup.find_all(class_="my_5div")

# Loop through the found elements and print
for element in elements_with_class:
    print(element)

Output

<div class="my_div">This is div 2.</div>
<div class="my_div">This is div 3.</div>
<div class="my_div">This is div 4.</div>

We have found three elements containing "my_div" class name in the class attribute.

Summary

In this Python BeautifulSoup tutorial, given the HTML content string, we have seen how to find the HTML elements containing specific class, using BeautifulSoup.find_all() method.