Beautiful Soup (HTML parser)
Beautiful Soup is a Python package for parsing HTML and XML documents (including having malformed markup, i.e. non-closed tags, so named after tag soup). It creates a parse tree for parsed pages that can be used to extract data from HTML[2], which is useful for web scraping.[1]
Original author(s) | Leonard Richardson |
---|---|
Stable release | 4.9.1
/ May 17, 2020 |
Repository | ![]() |
Written in | Python |
Platform | Python |
Type | HTML parser library, Web scraping |
License | Python Software Foundation License (Beautiful Soup 3 - an older version) MIT License 4+[1] |
Website | www |
It is available for Python 2.7 and Python 3.
Code example
#!/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', '/'))
Advantages and Disadvantages
This table summarizes the advantages and disadvantages of each parser library[1]
Parser | Typical usage | Advantages | Disadvantages |
---|---|---|---|
Python’s html.parser | BeautifulSoup(markup, "html.parser") |
|
|
lxml’s HTML parser | BeautifulSoup(markup, "lxml") |
|
|
lxml’s XML parser |
BeautifulSoup(markup, "lxml-xml") |
|
|
html5lib | BeautifulSoup(markup, "html5lib") |
|
|
Release
Beautiful Soup 3 was the official release line of Beautiful Soup from May 2006 to March 2012. The current release is Beautiful Soup 4.9.1 (May 17, 2020). You can install Beautiful Soup 4 with pip install beautifulsoup4
.
gollark: It technically has memory leaks if it happens to error during certain bits of parsing.
gollark: cmark and some horrible regexes.
gollark: One public wiki I like has "warp links" which identify "weird" links between somewhat disparate concepts, but that might work as well with just custom link text so hm.
gollark: ???
gollark: There is presumably *some* use for this other than making tags work without dedicated link features, but I can't really think what it is so æ.
See also
References
- "Beautiful Soup website". Retrieved 18 April 2012.
Beautiful Soup is licensed under the same terms as Python itself
- 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, retrieved 2020-04-11
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.