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

html code form:

<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

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

<p class="story">...</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 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>

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

>>> 

Tags: Python xml pip encoding

Posted on Mon, 02 Dec 2019 09:01:26 -0500 by jodyanne