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: