Beautiful soup is a python library that can extract data from HTML or XML files. It can implement the usual document navigation, searching, and modifying methods through your favorite converter. beautiful Soup helps you save hours or even days of work.
Quick Start. Use the following HTML as an example.
html_doc = """
Use beautifulsoup to parse this code and you can getBeautifulSoup
And can output according to the structure of the standard indent format:
from bs4 import BeautifulSoupsoup = BeautifulSoup(html_doc,‘html.parser‘)print(soup.prettify())
Several simple methods to browse structured data:
soup.title<title>The Dormouse‘s story</title>soup.title.name‘title‘soup.title.string"The Dormouse‘s story"soup.title.strings<generator object _all_strings at 0x0000025B5572A780>soup.title.parent.name‘head‘soup.p<p class="title"><b>The Dormouse‘s story</b></p>soup.p[‘class‘][‘title‘]soup.a<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>soup.find_all(‘a‘)[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]soup.find(id=‘link3‘)<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
Find the links to all <A> labels in the document:
for link in soup.find_all(‘a‘): print(link.get(‘href‘)) http://example.com/elsiehttp://example.com/laciehttp://example.com/tillie
Retrieve all text from the document:
print(soup.get_text())The Dormouse‘s storyThe Dormouse‘s storyOnce upon a time there were three little sisters; and their names wereElsie,Lacie andTillie;and they lived at the bottom of a well.
Beautiful Soup Module