1. Beautiful Soup的簡介
簡單來說,Beautiful Soup是python的一個庫,最主要的功能是從網頁抓取數據。官方解釋如下:
Beautiful Soup提供一些簡單的、python式的函數用來處理導航、搜索、修改分析樹等功能。它是一個工具箱,通過解析文檔爲用戶提供需要抓取的數據,因爲簡單,所以不需要多少代碼就可以寫出一個完整的應用程序。
Beautiful Soup自動將輸入文檔轉換爲Unicode編碼,輸出文檔轉換爲utf-8編碼。你不需要考慮編碼方式,除非文檔沒有指定一個編碼方式,這時,Beautiful Soup就不能自動識別編碼方式了。然後,你僅僅需要說明一下原始編碼方式就可以了。
Beautiful Soup已成爲和lxml、html6lib一樣出色的python解釋器,爲用戶靈活地提供不同的解析策略或強勁的速度。
廢話不多說,我們來試一下吧~
2. Beautiful Soup 安裝
Beautiful Soup 3 目前已經停止開發,推薦在現在的項目中使用Beautiful Soup 4,不過它已經被移植到BS4了,也就是說導入時我們需要 import bs4 。所以這裏我們用的版本是 Beautiful Soup 4.3.2 (簡稱BS4),另外據說 BS4 對 Python3 的支持不夠好,不過我用的是 Python2.7.7,如果有小夥伴用的是 Python3 版本,可以考慮下載 BS3 版本。
如果你用的是新版的Debain或Ubuntu,那麼可以通過系統的軟件包管理來安裝,不過它不是最新版本,目前是4.2.1版本
1
|
sudo
apt-get
install
Python-bs4
|
如果想安裝最新的版本,請直接下載安裝包來手動安裝,也是十分方便的方法。在這裏我安裝的是 Beautiful Soup 4.3.2
1
|
Beautiful Soup 3.2.1Beautiful Soup 4.3.2
|
下載完成之後解壓
運行下面的命令即可完成安裝
1
|
sudo
python setup.py
install
|
如下圖所示,證明安裝成功了

然後需要安裝 lxml
1
|
sudo
apt-get
install
Python-lxml
|
Beautiful Soup支持Python標準庫中的HTML解析器,還支持一些第三方的解析器,如果我們不安裝它,則 Python 會使用 Python默認的解析器,lxml 解析器更加強大,速度更快,推薦安裝。
3. 開啓Beautiful Soup 之旅
在這裏先分享官方文檔鏈接,不過內容是有些多,也不夠條理,在此本文章做一下整理方便大家參考。
官方文檔
4. 創建 Beautiful Soup 對象
首先必須要導入 bs4 庫
1
|
from
bs4
import
BeautifulSoup
|
我們創建一個字符串,後面的例子我們便會用它來演示
1
2
3
4
5
6
7
8
9
10
11
|
html
=
"""
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
|
創建 beautifulsoup 對象
1
|
soup
=
BeautifulSoup(html)
|
另外,我們還可以用本地 HTML 文件來創建對象,例如
1
|
soup
=
BeautifulSoup(
open
(
'index.html'
))
|
上面這句代碼便是將本地 index.html 文件打開,用它來創建 soup 對象
下面我們來打印一下 soup 對象的內容,格式化輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
print
soup.prettify()
<html>
<head>
<title>
The Dormouse's story
<
/
title>
<
/
head>
<body>
<p
class
=
"title"
name
=
"dromouse"
>
<b>
The Dormouse's story
<
/
b>
<
/
p>
<p
class
=
"story"
>
Once upon a time there were three little sisters;
and
their names were
<!
-
-
Elsie
-
-
>
<
/
a>
,
Lacie
<
/
a>
and
Tillie
<
/
a>
;
and
they lived at the bottom of a well.
<
/
p>
<p
class
=
"story"
>
...
<
/
p>
<
/
body>
<
/
html>
|
以上便是輸出結果,格式化打印出了它的內容,這個函數經常用到,小夥伴們要記好咯。
5. 四大對象種類
Beautiful Soup將複雜HTML文檔轉換成一個複雜的樹形結構,每個節點都是Python對象,所有對象可以歸納爲4種:
- Tag
- NavigableString
- BeautifulSoup
- Comment
下面我們進行一一介紹
(1)Tag
Tag 是什麼?通俗點講就是 HTML 中的一個個標籤,例如
1
2
3
|
<
title
>The Dormouse's story</
title
>
|
上面的 title a 等等 HTML 標籤加上裏面包括的內容就是 Tag,下面我們來感受一下怎樣用 Beautiful Soup 來方便地獲取 Tags
下面每一段代碼中註釋部分即爲運行結果
1
2
3
4
5
6
7
8
9
10
11
|
print
soup.title
#<title>The Dormouse's story</title>
print
soup.head
#<head><title>The Dormouse's story</title></head>
print
soup.a
print
soup.p
#<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
|
我們可以利用 soup加標籤名輕鬆地獲取這些標籤的內容,是不是感覺比正則表達式方便多了?不過有一點是,它查找的是在所有內容中的第一個符合要求的標籤,如果要查詢所有的標籤,我們在後面進行介紹。
我們可以驗證一下這些對象的類型
1
2
|
print
type
(soup.a)
#<class 'bs4.element.Tag'>
|
對於 Tag,它有兩個重要的屬性,是 name 和 attrs,下面我們分別來感受一下
1
2
3
4
5
6
|
name
print
soup.name
print
soup.head.name
#[document]
#head
|
soup 對象本身比較特殊,它的 name 即爲 [document],對於其他內部標籤,輸出的值便爲標籤本身的名稱。
1
2
3
4
|
attrs
print
soup.p.attrs
#{'class': ['title'], 'name': 'dromouse'}
|
在這裏,我們把 p 標籤的所有屬性打印輸出了出來,得到的類型是一個字典。
如果我們想要單獨獲取某個屬性,可以這樣,例如我們獲取它的 class 叫什麼
1
2
|
print
soup.p[
'class'
]
#['title']
|
還可以這樣,利用get方法,傳入屬性的名稱,二者是等價的
1
2
|
print
soup.p.get(
'class'
)
#['title']
|
我們可以對這些屬性和內容等等進行修改,例如
1
2
3
|
soup.p[
'class'
]
=
"newClass"
print
soup.p
#<p class="newClass" name="dromouse"><b>The Dormouse's story</b></p>
|
還可以對這個屬性進行刪除,例如
1
2
3
|
del
soup.p[
'class'
]
print
soup.p
#<p name="dromouse"><b>The Dormouse's story</b></p>
|
不過,對於修改刪除的操作,不是我們的主要用途,在此不做詳細介紹了,如果有需要,請查看前面提供的官方文檔
(2)NavigableString
既然我們已經得到了標籤的內容,那麼問題來了,我們要想獲取標籤內部的文字怎麼辦呢?很簡單,用 .string 即可,例如
1
2
|
print
soup.p.string
#The Dormouse's story
|
這樣我們就輕鬆獲取到了標籤裏面的內容,想想如果用正則表達式要多麻煩。它的類型是一個 NavigableString,翻譯過來叫 可以遍歷的字符串,不過我們最好還是稱它英文名字吧。
1
2
|
print
type
(soup.p.string)
#<class 'bs4.element.NavigableString'>
|
來檢查一下它的類型
1
2
|
print
type
(soup.p.string)
#<class 'bs4.element.NavigableString'>
|
printtype
(soup.p.string)