BeautifulSoup: Get class name of element ['class']

Syntax

element['class']

Example

from bs4 import BeautifulSoup

# Html source
html = """
<div>
<h2 class='recent'>Recent Posts:</h2>
</div>
"""

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

# Get h2 tag
h2 = soup.h2

# Print h2 class
print(h2['class'])

Output:

['recent']