Importing BeautifulSoup in Python

Oct 6, 2023 ยท 2 min read

The first step in any BeautifulSoup web scraping script is importing the module and initializing the soup object to parse the HTML content. This seemingly simple step has some key nuances to keep in mind:

Installation

Before importing BeautifulSoup, you need to install it via pip:

pip install beautifulsoup4

Make sure to install beautifulsoup4 rather than BeautifulSoup3 for the latest version.

Import

Then you can import BeautifulSoup into your Python script:

from bs4 import BeautifulSoup

The commonly used alias is just BeautifulSoup or bs4 for short.

Creating the Soup

To create a soup object, pass the HTML text and the parser to use:

soup = BeautifulSoup(html_text, 'html.parser')

BeautifulSoup can actually infer the parser to use automatically, but it's best to be explicit.

Handling Encodings

You may need to specify the original document encoding when creating the soup to prevent encoding issues:

soup = BeautifulSoup(html_text, 'html.parser', from_encoding='utf-8')

Alternatively, you can let BeautifulSoup auto-detect the encoding.

Loading from Files/URLs

Rather than direct HTML text, you can also load an HTML file from disk or from a remote URL:

soup = BeautifulSoup(open("index.html"), "html.parser")

soup = BeautifulSoup(requests.get(url).text, 'html.parser')

This initializes BeautifulSoup correctly from the given source ready for parsing and extraction.

So in summary, proper importing and initialization of BeautifulSoup is essential for robust web scraping in Python.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


Try ProxiesAPI for free

curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
...

X

Don't leave just yet!

Enter your email below to claim your free API key: