Beautifulsoup Get Attribute

Beautifulsoup Get Attribute

In this Beautifulsoup topic, we will learn how to:

  •  Get attributes of a tag
  • Get by Attribute Value
  • Get by existing attribute

Beautifulsoup Get attributes of a tag

To get tag' attributes, we'll use the .attrs property. The .attrs property returns the result as a dictionary.

Syntax

element.attrs

Example Get attributes of a tag

In the following example, we'll get the attributes of the div tag.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
html = '''

<div id="node">
<p>Hello Beautifulsoup</p>
</div>

'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find("div") # 👉️ Find <div>

div_attrs = el.attrs # 👉️ Get div Attributes

print(div_attrs) # 👉️ Print Result

Output:

{'id': 'node'}

As you can see, the program return ID attributes with its value. Let's see another example with ID and class attributes.

# 👇 HTML Source
html = '''

<div id="node" class="col">
<p>Hello Beautifulsoup</p>
</div>

'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find("div") # 👉️ Find <div>

div_attrs = el.attrs # 👉️ Get div Attributes

print(div_attrs) # 👉️ Print Result

Output:

{'id': 'node', 'class': ['col']}

The .attrs property always returns the value of the class attribute as a list because the class attribute may contain multi values.

Now, let's print the values of the class and ID attributes.

# 👇 HTML Source
html = '''

<div id="node" class="col">
<p>Hello Beautifulsoup</p>
</div>

'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find("div") # 👉️ Find <div>

div_attrs = el.attrs # 👉️ Get div Attributes

print("Class:", div_attrs["class"]) # 👉️ Print Class Attribute

print("ID:", div_attrs["id"]) # 👉️ Print ID Attribute

Output:

Class: ['col']
ID: node

Note: you can use this method to get any attribute. Check out Get the aria-label attribute to see how to get the aria-label attribute.

Beautifulsoup Get By Attribute Value

To get a tag by attribute value, we need to add the attrs parameter to the find() function:

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
html = '''

<div id="node">
<p>Hello Beautifulsoup</p>
</div>

'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find(attrs={"id" : "node"}) # 👉️ Get By Attribute Value

print(el) # 👉️ Print

Output:

<div id="node">
<p>Hello Beautifulsoup</p>
</div>

To find by multiple attributes value, follow the example below:

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
html = '''

<div id="node" class="root">
<p>Hello Beautifulsoup</p>
</div>
'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find(attrs={"id": "node", "class": ["root"]}) # 👉️ Get By Multiple Attribute Value

print(el) # 👉️ Print

Output:

<div class="root" id="node">
<p>Hello Beautifulsoup</p>
</div>

As you can see, we get the div that has Id=node and class=root.

Beautifulsoup Get By existing attribute

You can also get a tag if it's attribute exists by setting True. Let's see an example.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
html = '''

<div id="node">
<p>Hello Beautifulsoup</p>
</div>
'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find(attrs={"id": True}) # 👉️ Get IF Attribute ID exist

print(el) # 👉️ Print

Output:

<div id="node">
<p>Hello Beautifulsoup</p>
</div>

Another example:

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
html = '''

<div id="node">
<p>Hello Beautifulsoup</p>
</div>
'''

soup = BeautifulSoup(html, 'html.parser') # 👉️ Parsing

el = soup.find(attrs={"id": True, "class": True}) # 👉️ Get IF Attribute ID and Class exist

print(el) # 👉️ Print

Output:

None

Here, we got None because the class attribute does not exist in the div tag.

Conclusion

In this article, we learned about how to get attributes in Beautifulsoup. You can visit Understand How to Use the attribute in Beautifulsoup Python for more examples.

The tutorial examples are available on GitHub.

The tutorial examples are available in the following video:

Happy Coding </>