Find the tag with a given attribute value in an HTML document using BeautifulSoup
Last Updated :
26 Mar, 2021
Prerequisites: Beautifulsoup
In this article, we will discuss how beautifulsoup can be employed to find a tag with the given attribute value in an HTML document.
Approach:
- Import module.
- Scrap data from a webpage.
- Parse the string scraped to HTML.
- Use find() function to find the attribute and tag.
- Print the result.
Syntax: find(attr_name=”value”)
Below are some implementations of the above approach:
Example 1:
Python3
from bs4 import BeautifulSoup
markup =
soup = BeautifulSoup(markup, 'html.parser' )
div_bs4 = soup.find( id = "container" )
print (div_bs4.name)
|
Output:
div
Example 2:
Python3
from bs4 import BeautifulSoup
markup =
soup = BeautifulSoup(markup, 'html.parser' )
print (div_bs4.name)
|
Output:
a
Example 3:
Python3
from bs4 import BeautifulSoup
markup =
soup = BeautifulSoup(markup, 'html.parser' )
div_bs4 = soup.find( class_ = "gfg" )
print (div_bs4.name)
|
Output:
p
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...