A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/beautifulsoup-object-python-beautifulsoup/ below:

BeautifulSoup object - Python Beautifulsoup

BeautifulSoup object - Python Beautifulsoup

Last Updated : 25 Oct, 2020

BeautifulSoup 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. The BeautifulSoup object represents the parsed document as a whole. For most purposes, you can treat it as a Tag object.

Syntax:  BeautifulSoup(document, parser)

Parameters: This function accepts two parameters as explained below: 

Below given examples explain the concept of BeautifulSoup object in Beautiful Soup. 
Example 1: In this example, we are going to create a document with a BeautifulSoup object and print a tag.

Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup

# Initialize the object with a HTML page
soup = BeautifulSoup('''
    <html>
        <h2> Heading 1 </h2>
        <h1> Heading 2 </h1>
    </html>
    ''', "lxml")

# Get the whole h2 tag
tag = soup.h2

# Print the tag
print(tag)

Output: 

<h2> Heading 1 </h2>

Example 2: In this example, we are going to create a document with a BeautifulSoup object and then extract the attributes using attrs approach.

Python3
# Import Beautiful Soup
from bs4 import BeautifulSoup

# Initialize the object with a HTML page
soup = BeautifulSoup('''
    
        <h2 class="hello"> Heading 1 </h2>
        <h1> Heading 2 </h1>
    
    ''', "lxml")

# Get the whole h2 tag
tag = soup.h2

# Get the attribute
attribute = tag.attrs

# Print the output
print(attribute)

Output: 

{'class': ['hello']}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4