Python BeautifulSoup  - Get attributes of HTML Element


Python BeautifulSoup  - Get attributes of HTML Element

To get the attributes of HTML element in Python using BeautifulSoup, you can use Tag.attrs property. The attrs property returns a dictionary with attribute names as keys, and the attribute values as respective values for the keys.

1. Get attributes of a Div element using attrs property in Python

In the following program, we take a sample HTML content in html_content variable, find the HTML element whose id is "my_div", and then get the attributes of this element using attrs property.

Python Program

from bs4 import BeautifulSoup

# Sample HTML content
html_content = """
<html>
    <body>
        <div id="my_div" class="article sample">
            <h2>Welcome!</h2>
            <p>This is a paragraph.</p>
        </div>
        <div>This is another div.</div>
    </body>
</html>
"""

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

# Find the element
element = soup.find(id="my_div")

# Get attributes of element
attributes = element.attrs

print(attributes)

Output

{'id': 'mydiv', 'class': ['article', 'sample']}

These are the attributes of the element <div id="my_div" class="article sample">.

You can access the individual attributes using the key. Or you can iterate over the attributes just like you iterate over a dictionary.

In the following program, we get the 'id' and 'class' attributes using the key values, and print them to output.

Python Program

from bs4 import BeautifulSoup

# Sample HTML content
html_content = """
<html>
    <body>
        <div id="my_div" class="article sample">
            <h2>Welcome!</h2>
            <p>This is a paragraph.</p>
        </div>
        <div>This is another div.</div>
    </body>
</html>
"""

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

# Find the element
element = soup.find(id="my_div")

# Get attributes of element
attributes = element.attrs

# Get id from attributes
attr_id = attributes['id']

# Get class from attributes
attr_class = attributes['class']

print(f"id : {attr_id}")
print(f"class : {attr_class}")

Output

id : my_div
class : ['article', 'sample']

Summary

In this Python BeautifulSoup tutorial, given the HTML element, we have seen how to get all the attributes of this HTML content as a dictionary using Tag.attrs property.