How to Install Beautiful Soup in Python
-
Install
Beautiful Soup
Using Package Installer for Python (PIP) -
Install
Beautiful Soup
Using Advanced Package Tool (APT) -
Install
Beautiful Soup
by Manually Downloading Source Codes -
Install
Beautiful Soup
by Usingwget
andtar
in Linux
Python provides us with various libraries and modules to perform different tasks. Beautiful Soup
library is used in Python to scrape HTML and XML files.
This article will discuss different ways to install the Beautiful Soup
library in Python.
Install Beautiful Soup
Using Package Installer for Python (PIP)
To install the Beautiful Soup
library using PIP, you can execute the following command in the command line terminal on your machine.
pip install beautifulsoup4
The above command, after execution, will install the beautiful soup
library on your machine. If you’re using Python 2.x
and 3.x
on the same machine, the above command will install the beautiful soup
library into your Python 2.x
directory.
To install the beautiful soup
library in the Python 3.x
directory, you can run the following command in the command line terminal.
pip3 install beautifulsoup4
Install Beautiful Soup
Using Advanced Package Tool (APT)
APT is used to handle software packages efficiently. You will require the computer’s administrator password to install the beautiful soup
library using APT.
To install beautiful soup
using APT, you must execute the following command in the command line terminal.
sudo apt-get install python-bs4
Here, the sudo
keyword is used to run the command in superuser, i.e., administrator mode. Without using the sudo
command, you will be unable to install any software on your machine.
Again, if you have multiple Python versions installed on your machine, the above command will install the beautiful soup
library into Python 2’s directory. To install the beautiful soup
library in Python 3, you can use the following command.
sudo apt-get install python3-bs4
Install Beautiful Soup
by Manually Downloading Source Codes
To install the beautiful soup
library in Python using the source code, you first need to download the source code from this link. You will get a compressed file from the link.
-
After downloading the file, you must go to the directory where the file has been downloaded. Then, you need to extract the files from the downloaded compressed file.
-
Once you extract all the files, you need to go to the folder in which the files have been extracted. There, you will find a file named
setup.py
.You must run the command line terminal in the current directory where
setup.py
is saved. -
Next, you need to run the
setup.py
file using the following command in the terminal.python setup.py.
-
After running the above command, the
beautiful soup
library will be installed in your Python 2’s directory. To install thebeautiful soup
library in Python 3, you can runsetup.py
using Python 3 by executing the following command in the command line terminal.python3 setup.py
Remember to execute the commands by opening the terminal in the same directory where setup.py
is stored. Otherwise, you will face errors.
Install Beautiful Soup
by Using wget
and tar
in Linux
The wget
command executes a URL to download a file using the command line. Also, the tar
command is used to unzip a compressed file using the command line.
We will use the following steps to install the beautiful soup
library using the wget
command and the tar
command.
-
First, we will execute the
wget
command with the download link of the source code of thebeautiful soup
library, as shown below.wget https://www.crummy.com/software/BeautifulSoup/bs4/download/4.0/beautifulsoup4-4.0.0b3.tar.gz
-
After downloading the compressed file, we will use the
tar
command to extract the contents of the compressed file, as shown below.tar -xvzf beautifulsoup4-4.0.0b3.tar.gz
-
Once the
tar
command is executed, we will execute thesetup.py
file using the following command.python setup.py install
-
When the above command is executed, the
beautiful soup
library is installed in the directory of Python 2 if there are different Python versions installed on your machine. To install the library in the Python 3 directory, you can use the following command.python3 setup.py install
To verify the installation of the beautiful soup
module, you can use the import
statement in your program, as shown below.
import bs4
If executing the above statement in a Python program doesn’t cause any error, the beautiful soup
library has been successfully installed on your machine.
Out of all the methods discussed in this article, we suggest you use the package installer for Python (PIP) or Advanced Package Tool (APT) to install the beautiful soup
library on your machine. This will help you avoid errors like version mismatch or absence of dependencies because PIP and APT handle these things for you.
Aditya Raj is a highly skilled technical professional with a background in IT and business, holding an Integrated B.Tech (IT) and MBA (IT) from the Indian Institute of Information Technology Allahabad. With a solid foundation in data analytics, programming languages (C, Java, Python), and software environments, Aditya has excelled in various roles. He has significant experience as a Technical Content Writer for Python on multiple platforms and has interned in data analytics at Apollo Clinics. His projects demonstrate a keen interest in cutting-edge technology and problem-solving, showcasing his proficiency in areas like data mining and software development. Aditya's achievements include securing a top position in a project demonstration competition and gaining certifications in Python, SQL, and digital marketing fundamentals.
GitHub