How to Import BeautifulSoup in Python
Last Updated :
23 Jul, 2025
Beautiful Soup is a Python library used for parsing HTML and XML documents. It provides a simple way to navigate, search, and modify the parse tree, making it valuable for web scraping tasks. In this article, we will explore how to import BeautifulSoup in Python.
What is BeautifulSoup?
BeautifulSoup is a Python library used for parsing HTML and XML documents. It creates a parse tree that allows you to navigate and manipulate elements within these documents effortlessly. This library is commonly used for web scraping tasks, where data needs to be extracted from web pages. BeautifulSoup handles malformed HTML well and provides a convenient interface to extract data by searching for tags, attributes, and more complex patterns within the document structure.
Import BeautifulSoup in Python
Below, we will explain step-by-step how to import BeautifulSoup in Python.
Create a Virtual Environment
Open VSCode and navigate to the directory where you want to work. Create a virtual environment using the terminal in VSCode.
Install BeautifulSoup Library
With the virtual environment activated, install BeautifulSoup using pip:
pip install beautifulsoup4
BeautifulSoup Installation using PipImport BeautifulSoup in Python Script
Once installed, you can import BeautifulSoup into your Python script or interactive session.
Python
from bs4 import BeautifulSoup
Verify BeautifulSoup Import
New we will write a simple Python script to verify if the BeautifulSoup module is successfully imported in Python or not. Let us see a few different examples of using the module.
Extracting Text from HTML
In this example, we use requests to fetch the HTML content of a webpage about Python from GeeksforGeeks. Using BeautifulSoup, we parse this HTML and demonstrate accessing and printing the title tag of the webpage, its tag name (title), and the name of its parent tag (head).
Python
# import modules
import requests
from bs4 import BeautifulSoup
# requrst to the ULR
r = requests.get('https://www.geeksforgeeks.org/python/python-programming-language-tutorial/')
# parsing HTML content
soup = BeautifulSoup(r.content, 'html.parser')
# printing details from the HTML
print(soup.title)
print(soup.title.name)
print(soup.title.parent.name)
Output:
Extracting text from HTML using BeautifulSoupParsing HTML String
In this example, we are parsing a simple HTML string (html_doc) using BeautifulSoup. It extracts the title, the text of the first paragraph (<p>), and all items in a list (<ul>).
Python
# import module
from bs4 import BeautifulSoup
# HTML document
html_doc = """
<html>
<head>
<title>GeeksforGeeks</title>
</head>
<body>
<h1>Welcome to GFG!</h1>
<p>This is BeautifulSoup Example</p>
<ul>
<li>Python</li>
<li>Java</li>
<li>C++</li>
</ul>
</body>
</html>
"""
# parsing HTML document
soup = BeautifulSoup(html_doc, 'html.parser')
# extracting details
title = soup.title
print(f"Title: {title.text}")
paragraph = soup.p
print(f"First Paragraph: {paragraph.text}")
list_items = soup.find_all('li')
print("List Items:")
for item in list_items:
print(item.text)
Output:
Parsing HTML String using BeautifulSoupConclusion
In conclusion, importing BeautifulSoup in Python is easier process that begins with creating a virtual environment and installing the library using pip. Once imported, BeautifulSoup allows efficient parsing, navigation, and extraction of data from HTML and XML documents, making it essential for web scraping tasks and beyond.
Similar Reads
How to Install BeautifulSoup in Python on MacOS? In this article, we will learn how to install Beautiful Soup in Python on MacOS. InstallationMethod 1: Using pip to install BeautifulSoup Step 1: Install latest Python3 in MacOS Step 2: Check if pip3 and python3 are correctly installed. python3 --version pip3 --version Step 3: Upgrade your pip to av
1 min read
How to Use lxml with BeautifulSoup in Python In this article, we will explore how to use lxml with BeautifulSoup in Python. lxml is a high-performance XML and HTML parsing library for Python, known for its speed and comprehensive feature set. It supports XPath, XSLT, validation, and efficient handling of large documents, making it a preferred
3 min read
How to Install BeautifulSoup in Jupyter Notebook Installation of BeautifulSoup on Jupyter Notebook is quite easy, and you will be all set for excellent web scraping and data extraction. It is a Python library that makes HTML and XML dealing with web data. It will help you get up and running with BeautifulSoup inside your Jupyter Notebook, so you c
4 min read
BeautifulSoup4 Module - Python BeautifulSoup4 is a user-friendly Python library designed for parsing HTML and XML documents. It simplifies the process of web scraping by allowing developers to effortlessly navigate, search and modify the parse tree of a webpage. With BeautifulSoup4, we can extract specific elements, attributes an
3 min read
How to Remove tags using BeautifulSoup in Python? Prerequisite- Beautifulsoup module In this article, we are going to draft a python script that removes a tag from the tree and then completely destroys it and its contents. For this, decompose() method is used which comes built into the module. Syntax: Beautifulsoup.Tag.decompose() Tag.decompose() r
2 min read
Contents list - Python Beautifulsoup The contents list 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 content is a list that contains the tagâs children.Syntax:Â tag.contents Below given exa
1 min read