Beautiful Soup (HTML parser)


Beautiful Soup is a Python package for parsing HTML and XML documents, including those with malformed markup. It creates a parse tree for documents that can be used to extract data from HTML, which is useful for web scraping.

History

Beautiful Soup was started in 2004 by Leonard Richardson. It takes its name from the poem Beautiful Soup from Alice's Adventures in Wonderland and is a reference to the term "tag soup" meaning poorly-structured HTML code. Richardson continues to contribute to the project, which is additionally supported by paid open-source maintainers from the company Tidelift.

Versions

Beautiful Soup 3 was the official release line of Beautiful Soup from May 2006 to March 2012. The current release is .
In 2021, Python 2.7 support was retired and the release 4.9.3 was the last to support Python 2.7.

Usage

Beautiful Soup represents parsed data as a tree which can be searched and iterated over with ordinary Python loops.

Code example

The example below uses the Python standard library's urllib to load Wikipedia's main page, then uses Beautiful Soup to parse the document and search for all links within.

  1. !/usr/bin/env python3
  2. Anchor extraction from HTML document
from bs4 import BeautifulSoup
from urllib.request import urlopen
with urlopen as response:
soup = BeautifulSoup
for anchor in soup.find_all:
print

Another example is using the Python requests library to get divs on a URL.

import requests
from bs4 import BeautifulSoup
url = "https://wikipedia.com"
response = requests.get
soup = BeautifulSoup
headings = soup.find_all
for heading in headings:
print)