Prerequisite: Beautifulsoup Installation
Attributes are provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. A tag may have any number of attributes. For example, the tag <b class=”active”> has an attribute “class” whose value is “active”. We can access a tag’s attributes by treating it like a dictionary.
Syntax:
tag.attrs
Implementation: Example 1: Program to extract the attributes using attrs approach.
Python3
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml")
tag = soup.h2
attribute = tag.attrs
print(attribute)
Output:
{'class': ['hello']}
Example 2: Program to extract the attributes using dictionary approach.
Python3
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml")
tag = soup.h2
attribute = tag['class']
print(attribute)
Output:
['hello']
Example 3: Program to extract the multiple attribute values using dictionary approach.
Python3
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml")
tag = soup.h2
attribute = tag['class']
print(attribute)
Output:
['first', 'second', 'third']
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Dec, 2020
Like Article
Save Article