Open In App

Tag Object – Python Beautifulsoup

Last Updated : 05 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Initialize the object with an HTML page
soup = BeautifulSoup('''
    <html>
        <h1>a web page</h1>
    </html>
    ''', "lxml")
  
# Get the tag
tag = soup.h1
  
# Print the output
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.




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Initialize the object with an HTML page
soup = BeautifulSoup('''
    <html>
        <strong>a web page</strong>
    </html>
    ''', "lxml")
  
# Get the tag
tag = soup.strong
  
# Print the output
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.




# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Initialize the object with a HTML page
soup = BeautifulSoup('''
    <html>
        <strong>the first strong tag</strong>
        <h1> Heading </h1>
        <strong>the second strong tag</strong>
    </html>
    ''', "lxml")
  
# Get the tag
tag = soup.strong
  
# Print the output
print(tag)


Output:

<strong>the first strong tag</strong>


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads