Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
Beautiful Soup (HTML parser)
Python HTML/XML 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.

We don't have any images related to Beautiful Soup (HTML parser) yet.
We don't have any YouTube videos related to Beautiful Soup (HTML parser) yet.
We don't have any PDF documents related to Beautiful Soup (HTML parser) yet.
We don't have any Books related to Beautiful Soup (HTML parser) yet.
We don't have any archived web articles related to Beautiful Soup (HTML parser) yet.

History

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

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.8

Usage

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

Code example

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

#!/usr/bin/env python3 # Anchor extraction from HTML document from bs4 import BeautifulSoup from urllib.request import urlopen with 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 library11 to get divs on a URL.

import requests from 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

References

  1. Hajba, Gábor László (2018), Hajba, Gábor László (ed.), "Using Beautiful Soup", Website Scraping with Python: Using BeautifulSoup and Scrapy, Apress, pp. 41–96, doi:10.1007/978-1-4842-3925-4_3, ISBN 978-1-4842-3925-4 978-1-4842-3925-4

  2. "Beautiful Soup website". Retrieved 18 April 2012. Beautiful Soup is licensed under the same terms as Python itself http://www.crummy.com/software/BeautifulSoup/#Download

  3. Python, Real. "Beautiful Soup: Build a Web Scraper With Python – Real Python". realpython.com. Retrieved 2023-06-01. https://realpython.com/beautiful-soup-web-scraper-python/

  4. makcorps (2022-12-13). "BeautifulSoup tutorial: Let's Scrape Web Pages with Python". Retrieved 2024-01-24. https://www.scrapingdog.com/blog/beautifulsoup-tutorial-web-scraping-with-python/

  5. "Python Web Scraping". Udacity. 2021-02-11. Retrieved 2024-01-24. https://www.udacity.com/blog/2021/02/python-web-scraping.html

  6. "Code : Leonard Richardson". Launchpad. Retrieved 2020-09-19. https://code.launchpad.net/%7Eleonardr/+branches

  7. Tidelift. "beautifulsoup4 | pypi via the Tidelift Subscription". tidelift.com. Retrieved 2020-09-19. https://tidelift.com/subscription/pkg/pypi-beautifulsoup4

  8. Richardson, Leonard (7 Sep 2021). "Beautiful Soup 4.10.0". beautifulsoup. Google Groups. Retrieved 27 September 2022. https://groups.google.com/g/beautifulsoup/c/flWqqlrcJ9s

  9. "How To Scrape Web Pages with Beautiful Soup and Python 3 | DigitalOcean". www.digitalocean.com. Retrieved 2023-06-01. https://www.digitalocean.com/community/tutorials/how-to-scrape-web-pages-with-beautiful-soup-and-python-3

  10. Python, Real. "Python's urllib.request for HTTP Requests – Real Python". realpython.com. Retrieved 2023-06-01. https://realpython.com/urllib-request/

  11. Blog, SerpApi (5 March 2024). "Beautiful Soup: Web Scraping with Python". serpapi.com. Retrieved 2024-06-27. https://serpapi.com/blog/beautiful-soup-build-a-web-scraper-with-python/