Last modified: March 22, 2021

BeautifulSoup: Get the aria-label attribute

Today, I'm going to show you how to get an aria-label attribute using BeautifulSoup.

Syntax


['aria-label']

Getting the aria-label attribute with example

In the following example, we'll get the aria-label attribute of the span tag.


from bs4 import BeautifulSoup

html = '''
<div>
<span class="span" aria-label="4 people reacted to this post" role="button"></span>
</div>
'''

soup = BeautifulSoup(html, 'html.parser')

#find Span
f = soup.find('span')

#Get aria-label attribute of Span
print(f['aria-label'])

Output:

4 people reacted to this post

Note: You can also get any specific attribute.

In the following example, let's get the role attribute.


print(f['role'])

Output:

button