A quick start to the tutorial 1 of beautiful soup, a python data extraction tool

brief introduction Picture.png Beautiful Soup is a library that can easily grab information from a web page. It is located at the top of an HTML or X...

brief introduction

Picture.png

Beautiful Soup is a library that can easily grab information from a web page. It is located at the top of an HTML or XML parser, providing Python style iterations, searching, and modifying parse trees.

install

pip install -U beautifulsoup4

quick get start

Picture.png
>>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup("<p>Some<b>bad<i>HTML", 'html.parser') >>> print(soup.prettify()) <p> Some <b> bad <i> HTML </i> </b> </p> >>> soup.find(text="bad") 'bad' >>> soup.i <i>HTML</i> >>> soup = BeautifulSoup("<tag1>Some<tag2/>bad<tag3>XML", "xml") >>> print(soup.prettify()) <?xml version="1.0" encoding="utf-8"?> <tag1> Some <tag2/> bad <tag3> XML </tag3> </tag1> >>>

reference material: https://pypi.org/project/beautifulsoup4/

Reference material

Slightly deeper

Picture.png

Beautiful Soup A Python library that can extract data from HTML or XML files. Through your favorite converter to achieve document navigation, search, modify. . Beautiful Soup will save you hours or days

  • Loading documents

This sample document from Alice in Wonderland story:

>>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup(html_doc, 'html.parser') >>> print(soup.prettify()) <html> <head> <title> The Dormouse's story </title> </head> <body> <p> <b> The Dormouse's story </b> </p> <p> Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" id="link1"> Elsie </a> , <a href="http://example.com/lacie" id="link2"> Lacie </a> and <a href="http://example.com/tillie" id="link3"> Tillie </a> ; and they lived at the bottom of a well. </p> <p> ... </p> </body> </html> >>>

html code form:

<html><head><title>The Dormouse's story</title></head> <body> <p><b>The Dormouse's story</b></p> <p>Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" id="link1">Elsie</a>, <a href="http://example.com/lacie" id="link2">Lacie</a> and <a href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p>...</p>

Effect of browser opening:

Picture.png
  • A simple way to navigate the data structure:
>>> soup.title <title>The Dormouse's story</title> >>> soup.title.name 'title' >>> soup.title.string "The Dormouse's story" >>> soup.title.parent.name 'head' >>> soup.p <p><b>The Dormouse's story</b></p> >>> soup.p['class'] ['title'] >>> soup.a <a href="http://example.com/elsie" id="link1">Elsie</a> >>> soup.find_all('a') [<a href="http://example.com/elsie" id="link1">Elsie</a>, <a href="http://example.com/lacie" id="link2">Lacie</a>, <a href="http://example.com/tillie" id="link3">Tillie</a>] >>> soup.find(id="link3") <a href="http://example.com/tillie" id="link3">Tillie</a>

Common operations: 1. Find all links through the < a > tag. 2. Extract all text from the page

>>> for link in soup.find_all('a'): ... print(link.get('href')) ... http://example.com/elsie http://example.com/lacie http://example.com/tillie >>> print(soup.get_text()) The Dormouse's story The Dormouse's story Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well. ... >>>

2 December 2019, 09:01 | Views: 3374

Add new comment

For adding a comment, please log in
or create account

0 comments