Last modified: Jan 10, 2023 By Alexander Williams
BeautifulSoup: Get Type Of Element
In this BeautifulSoup lesson, we're going to learn how to get the type of any element.
Syntax
el.name
Get the type of an element with example
from bs4 import BeautifulSoup
# html source
html = """
<div>
<h1 class="el">This is H1</h1>
<h2 class="el">This is H2</h2>
<h3 class="el">This is H3</h3>
<div>
"""
# BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# Find all with "el" class
els = soup.find_all(class_="el")
# Print Element type
for el in els:
print(el.name)
Output:
h1 h2 h3