How To Install BeautifulSoup
To install BeautifulSoup on a MacOS or Linux machines run:
sudo pip3 install beautifulsoup4
To install BeautifulSoup on a Windows machine run:
pip3 install beautifulsoup4
To import BeautifulSoup into your Python script:
from bs4 import BeautifulSoup
Then to use BeautifulSoup to parse a HTML file, simply initialize a BeautifulSoup instance with the HTML file.
from bs4 import BeautifulSoup
html_doc = """
<html>
<body>
<h1>Hello!</h1>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.find('h1').get_text())
# --> 'Hello!'
In this guide for The Python Web Scraping Playbook, we will look at how to install and use Python's popular BeautifulSoup library on Windows, MacOS, Linux machines.
We will walk your through:
- How To Install BeautifulSoup On MacOS & Linux
- How To Install BeautifulSoup On Windows
- How To Import & Setup BeautifulSoup
First, let's get a quick overview of what is BeautifulSoup.
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
How To Install BeautifulSoup On MacOS & Linux
Installing Python's BeautifulSoup on a MacOS machine is very straightforward.
Step 1: Install Latest Python Version
The first step is to make sure you have the latest version of Python3 installed on your MacOS machine.
To do so, run the following command in your terminal:
python3 --version
>> Python 3.11.0
Step 2: Upgrade Pip
Next upgrade pip to the latest version to avoid any installation issues.
sudo pip3 install --upgrade pip
Step 3: Install BeautifulSoup
Finally, we just need to install BeautifulSoup.
sudo pip3 install beautifulsoup4
How To Install BeautifulSoup On Windows
Installing Python's BeautifulSoup on a Windows machine is very straightforward.
Step 1: Install Latest Python Version
The first step is to make sure you have the latest version of Python3 installed on your Windows machine.
To do so, run the following command in your terminal:
python3 --version
>> Python 3.11.0
Step 2: Upgrade Pip
Next upgrade pip to the latest version to avoid any installation issues.
pip3 install --upgrade pip
Step 3: Install BeautifulSoup
Finally, we just need to install BeautifulSoup.
pip3 install beautifulsoup4
How To Import & Setup BeautifulSoup
To import BeautifulSoup into your Python script import BeautifulSoup
from the bs4
module:
from bs4 import BeautifulSoup
Then to use BeautifulSoup to parse a HTML file, simply initialize a BeautifulSoup instance with the HTML file.
from bs4 import BeautifulSoup
html_doc = """
<html>
<body>
<h1>Hello!</h1>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'html.parser')
print(soup.find('h1').get_text())
# --> 'Hello!'
More Web Scraping Tutorials
So that's how to install Python BeautifulSoup.
If you would like to learn more about how to use BeautifulSoup then check out our other BeautifulSoup guides:
- BeautifulSoup Guide: Scraping HTML Pages With Python
- Fix BeautifulSoup Returns Empty List or Value
- How To Use BeautifulSoup's find() Method
- How To Use BeautifulSoup's find_all() Method
Or if you would like to learn more about Web Scraping, then be sure to check out The Python Web Scraping Playbook.
Or check out one of our more in-depth guides: