Last modified: Jan 10, 2023 By Alexander Williams
Beautifulsoup Get Class Name
Β To get the class name of an element in Beautifulsoup, you need to use the following syntax:
element['class']
By using this syntax, we'll learn how to:
- Get a class name of an element
- Get multi-class names of an element
- Get the class name of multi-elements
Get a class name of an element
In the following example, we'll get the class name of the div element.
from bs4 import BeautifulSoup # ποΈ Import BeautifulSoup module
# π HTML Source
html = '''
<div class="node">
<p>Hello Beautifulsoup</p>
</div>
'''
soup = BeautifulSoup(html, 'html.parser') # ποΈ Parsing
el = soup.find("div") # ποΈ Find <div>
div_class = el['class'] # ποΈ Get div class name
print(div_class) # ποΈ Print Result
Output:
['node']
As you can see, we got the result as a list because an element may contain multiple class names. If you want to get the class name inside the list, follow the code below.
if div_class: # ποΈ Check if DIV has a class attr
print(div_class[0]) # ποΈ Print Class name
Output:
node
We've used the if condition because we'll get an error if the element does not have the class attribute. However, You can set the class_=True parameter at the find() function instead of the if condition.
By setting theΒ class_=True parameter, we tell the function that we want to find the element that has a class attribute.
# π HTML Source
html = '''
<div class="node">
<p>Hello Beautifulsoup</p>
</div>
'''
soup = BeautifulSoup(html, 'html.parser') # ποΈ Parsing
el = soup.find("div", class_=True) # ποΈ Find <div>
div_class = el['class'] # ποΈ Get div class name
print(div_class[0]) # ποΈ Print Class name
Output:
node
Get multi-class names of an element
In the following example, we'll see how to work with the element that has multi-class names.
# π HTML Source
html = '''
<div class="node text fav">
<p>Hello Beautifulsoup</p>
</div>
'''
soup = BeautifulSoup(html, 'html.parser') # ποΈ Parsing
el = soup.find("div") # ποΈ Find <div>
div_class = el['class'] # ποΈ Get div class name
print(div_class) # ποΈ Print Result
Output:
['node', 'text', 'fav']
Now, let's print each class name.
# # π Print each class name
for _class in div_class:
print(_class)
Output:
node
text
fav
Get the class name of multi-elements
The following example will explain how toΒ get the class name of multi-elements.
# π HTML Source
html = '''
<div class="node">
<p>Hello Beautifulsoup</p>
</div>
<div class="node_1">
<p>Hello Beautifulsoup</p>
</div>
<div class="node_2">
<p>Hello Beautifulsoup</p>
</div>
<div>
<p>Hello Beautifulsoup</p>
</div>
'''
soup = BeautifulSoup(html, 'html.parser') # ποΈ Parsing
els = soup.find_all("div", class_=True) # ποΈ Find all <div> tags with existing class attr
for el in els: # ποΈ Loop over els
print(el['class']) # ποΈ Print Class name
Output:
['node']
['node_1']
['node_2']
Conclusion
We've covered everything about getting a class name from an element or multi-elements. For more Beatifoulsoup topics, scroll down and happy learning.
The tutorial's Examples are available on GitHub.
You can also watch the tutorial's video: