Tag Object – Python Beautifulsoup
Last Updated :
05 Sep, 2020
Tag object is 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. Tag object corresponds to an XML or HTML tag in the original document. Further, this object is usually used to extract a tag from the whole HTML document. Further, Beautiful Soup is not an HTTP client which means to scrap online websites you first have to download them using requests module and then serve it to Beautiful Soup for scraping. Additionally, this object returns the first found tag if your document have multiple tags with the same name.
Prerequisite: Beautiful Soup Installation
Syntax:
Object.tag_name
Parameters: This function doesn’t accept any parameter.
Below given examples explain the concept of Tag object in Beautiful Soup.
Example 1: In this example we are going to extract h1 tag element only.
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml" )
tag = soup.h1
print (tag)
|
Output:
<h1>a web page</h1>
Example 2: In this example we are going to see the type of strong tag element only.
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml" )
tag = soup.strong
print ( type (tag))
|
Output:
<class 'bs4.element.Tag'>
Example 3: In this example we are going to see the output from a HTML with multiple tags.
from bs4 import BeautifulSoup
soup = BeautifulSoup(
, "lxml" )
tag = soup.strong
print (tag)
|
Output:
<strong>the first strong tag</strong>
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...