Python - Get specific attribute value of HTML Element
Python BeautifulSoup - Get specific attribute value of HTML Element
To get the specific attribute value 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. Access this dictionary with the required attribute as key.
1. Get id attribute 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 id attribute 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
# Get id from attributes
attr_id = attributes['id']
print(attr_id)
Output
my_div
The id attribute of the element <div id="my_div" class="article sample">
is my_div
.
2. Get class attribute of a Div element using attrs property in Python
In the following program, we get the class attribute 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
# Get class from attributes
attr_class = attributes['class']
print(attr_class)
Output
['article', 'sample']
Since the class attribute can contain multiple class names, it is returned as a list. You can join this list to a string with space as separator using the following statement.
attr_class = " ".join(attributes['class'])
instead of
attr_class = attributes['class']
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.