Beautiful Soup (HTML parser) explained

Beautiful Soup
Beautiful Soup
Author:Leonard Richardson
Programming Language:Python
Platform:Python
Genre:HTML parser library, Web scraping

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.[1]

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[2] and is a reference to the term "tag soup" meaning poorly-structured HTML code.[3] Richardson continues to contribute to the project,[4] which is additionally supported by paid open-source maintainers from the company Tidelift.[5]

Versions

Beautiful Soup 3 was the official release line of Beautiful Soup from May 2006 to March 2012. The current release is Beautiful Soup 4.x.

In 2021, Python 2.7 support was retired and the release 4.9.3 was the last to support Python 2.7.[6]

Usage

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

Code example

The example below uses the Python standard library's urllib[8] 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 BeautifulSoupfrom urllib.request import urlopenwith urlopen('https://en.wikipedia.org/wiki/Main_Page') as response: soup = BeautifulSoup(response, 'html.parser') for anchor in soup.find_all('a'): print(anchor.get('href', '/'))

Another example is using the Python requests library[9] to get divs on a URL.import requestsfrom bs4 import BeautifulSoup

url = 'https://wikipedia.com'response = requests.get(url)soup = BeautifulSoup(response.text, 'html.parser')headings = soup.find_all('div')

for heading in headings: print(heading.text.strip)

See also

Notes and References

  1. Web site: Python . Real . Beautiful Soup: Build a Web Scraper With Python – Real Python . 2023-06-01 . realpython.com . en.
  2. Web site: makcorps . 2022-12-13 . BeautifulSoup tutorial: Let's Scrape Web Pages with Python . 2024-01-24 . en-US.
  3. Web site: 2021-02-11 . Python Web Scraping . 2024-01-24 . Udacity . en-US.
  4. Web site: Code : Leonard Richardson . 2020-09-19 . Launchpad . en-US.
  5. Web site: Tidelift. beautifulsoup4 pypi via the Tidelift Subscription. 2020-09-19. tidelift.com. en.
  6. Web site: Richardson . Leonard . 7 Sep 2021 . Beautiful Soup 4.10.0 . 27 September 2022 . beautifulsoup . Google Groups . en-US.
  7. Web site: How To Scrape Web Pages with Beautiful Soup and Python 3 DigitalOcean . 2023-06-01 . www.digitalocean.com . en.
  8. Web site: Python . Real . Python's urllib.request for HTTP Requests – Real Python . 2023-06-01 . realpython.com . en.
  9. Web site: Blog . SerpApi . Beautiful Soup: Web Scraping with Python. 2024-06-27 . serpapi.com . en.