文章目錄
一、BeautifulSoup 4介紹
1.1 簡介
簡單來說,BeautifulSoup是python的一個庫,最主要的功能是從網頁抓取數據。官方解釋如下:
Beautiful Soup提供一些簡單的、python式的函數用來處理導航、搜索、修改分析樹等功能。它是一個工具箱,通過解析文檔爲用戶提供需要抓取的數據。
Beautiful Soup能將輸入文檔轉換爲Unicode編碼,輸出文檔轉換爲UTF-8編碼。如果文檔沒有說明編碼方式, Beautiful Soup就需要自動設置編碼方式。
1.2 解析庫對比
序號 | 解析庫 | 使用方法 | 優勢 | 劣勢 |
---|---|---|---|---|
1 | Python標準庫(內置庫) | BeautifulSoup(html,“html.parser”) | Python內置標準庫,執行速度快 | 容錯能力較差 |
2 | lxml HTML解析庫 | BeautifulSoup(html,’lxml’) | 速度快,容錯能力強 | 需要安裝,需要C語言庫 |
lxml XML解析庫 | BeautifulSoup(html,[‘lxml’,’xml’]) | 速度快,容錯能力強,支持XML格式 | 需要C語言庫 | |
4 | htm5lib解析庫 | BeautifulSoup(html,’htm5llib’) | 以瀏覽器方式解析,最好的容錯性 | 速度慢 |
二、BeautifulSoup 4基本使用
2.1 安裝和文檔
pip install Beautifulsoup4
中文文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
2.2 導入使用
form bs4 import BeautifulSoup
2.3 基礎操作實例
from bs4 import BeautifulSoup
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
<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>
"""
soup = BeautifulSoup(html,'lxml')
# prettify() 方法 格式化輸出 ; 將Beautiful Soup的文檔樹格式化後以Unicode編碼輸出,每個XML/HTML標籤都獨佔一行
print(soup.prettify())
三、四大對象種類
Beautiful Soup將複雜HTML文檔轉換成一個複雜的樹形結構,每個節點都是Python對象,所有對象可以歸納爲4種:
序號 | 對象 | 作用 |
---|---|---|
1 | Tag | BeautifulSoup中所有的標籤都是Tag類型,並且BeautifulSoup的對象其實本質上也是一個Tag類型。所以其實一些方法比如find、find_all並不是BeautifulSoup的,而是Tag的。 tag對應Html中的標籤 |
2 | NavigableString | 獲取標籤內的標籤內部的文字,它繼承自python中的str,用起來就跟使用python的str是一樣的。 |
3 | BeautifulSoup | BeautifulSoup 對象表示的是一個文檔的內容。也可以認爲它是一個特殊的 Tag |
4 | Comment | Comment 對象是一個特殊類型的 NavigableString 對象,可以獲取文檔中註釋節點的內容。 |
3.1 Tag 實例
下面的 title
head
a
p
等等 HTML 標籤加上裏面包括的內容就是 Tag,,但是注意它查找的是在所有內容中的第一個符合要求的標籤。
<head><title>The Dormouse's story</title></head>
<a class="sister" href="http://example.com/elsie" id="link1"><!-- Elsie --></a>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
獲取tag
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b><!--Hey, buddy. Want to buy a used parser?--></b>
"""
soup = BeautifulSoup(html,'lxml')
# 獲取 tag
print(soup.p) # <p class="title" name="dromouse"></p>
# 獲取p標籤的屬性
print(type(soup.p)) # <class 'bs4.element.Tag'>
對於 Tag,它有兩個重要的屬性,是 name 和 attrs
# 獲取標籤名
print(soup.p.name) # p
# 獲取標籤名所有屬性
print(soup.p.attrs) # {'class': ['title'], 'name': 'dromouse'}
# 獲取P標籤下class的值
print(soup.p['class']) # ['title']
print(soup.p.get('class')) # ['title']
3.2 NavigableString 實例
獲取標籤內部的文字用 .string 即可,例如:
from bs4.element import NavigableString
# 獲取a標籤內的文字內容
print(soup.a.string) # Elsie
# 判斷a.string的類型
print(type(soup.a.string)) # <class 'bs4.element.Comment'>
3.3 BeautifulSoup 實例
是一個特殊的 Tag,我們可以分別獲取它的類型,名稱,以及屬性,例如:
# BeautifulSoup的類型,名稱以及屬性
print(type(soup))
# <class 'bs4.BeautifulSoup'>
print(type(soup.name))
# <class 'str'>
print(soup.name)
# [document] ; 每個HTML文檔都是Document。
print(soup.attrs)
# {} 文檔本身的屬性爲空
3.4 Comment 實例
它繼承NavigableString對象,所有使用方法和NavigableString對象一樣,通過.string使用
print(soup.b) # 此時不能出現空格和換行符,a標籤如下:
# <b><!--Hey, buddy. Want to buy a used parser?--></b>
print(soup.b.string) # Hey, buddy. Want to buy a used parser?
print(type(soup.b.string)) # <class 'bs4.element.Comment'>
四、string和strings、stripped_strings屬性以及get_text方法
- string:獲取某個標籤下的非標籤字符串。返回來的是個字符串。如果這個標籤下有多行字符,那麼就不能獲取到了。
- strings:獲取某個標籤下的子孫非標籤字符串。返回來的是個生成器。
- stripped_strings:獲取某個標籤下的子孫非標籤字符串,會去掉空白字符。返回來的是個生成器。
- get_text:獲取某個標籤下的子孫非標籤字符串,以普通字符串形式返回
五、遍歷文檔樹
5.1 直接子節點
5.1.1 content 屬性
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
"""
soup = BeautifulSoup(html,'lxml')
head_tag = soup.head
print(head_tag.contents) # .contents 調用
# [<title>The Dormouse's story</title>]
print(type(head_tag.contents))
# <class 'list'>
5.1.2 children屬性
print(type(head_tag.children)) # 列表迭代器 <class 'list_iterator'>
for i in head_tag.children:
print(i)
# <title>The Dormouse's story</title>
5.1.3 contents和children的區別
都是返回某個標籤下的直接子元素,其中也包括字符串。他們兩的區別是:contents返回來的是一個列表,children返回的是一個迭代器。
5.2 所有子(孫)節點
5.2.1 descendants 屬性
.descendants 屬性可以對所有tag的子孫節點進行遞歸循環,和 children類似,我們也需要遍歷獲取其中的內容。
soup = BeautifulSoup(html,'lxml')
for child in soup.descendants:
print(child)
運行結果:
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
</body></html>
<head><title>The Dormouse's story</title></head>
<title>The Dormouse's story</title>
The Dormouse's story
<body>
<p class="title" name="dromouse"></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>
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
</body>
<p class="title" name="dromouse"></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>
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>
Elsie
,
<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>
Lacie
and
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>
Tillie
;
and they lived at the bottom of a well.
<p class="story">...</p>
...
<b>
<!--Hey, buddy. Want to buy a used parser?-->
</b>
Hey, buddy. Want to buy a used parser?
5.3 返回所有字符類型
string:返回所有字符類型
for child in soup.body.descendants:
print( repr(child.string))
# repr() 方法是一個函數,創建一個字符串,以合法python表達式的形式來表示值。,repr出來的值是給機器看的,能顯示出文檔內的換行符空格符等特殊字符
結果:
'\n'
None
'\n'
None
'Once upon a time there were three little sisters; and their names were\n'
' Elsie '
' Elsie '
',\n'
'Lacie'
'Lacie'
' and\n'
'Tillie'
'Tillie'
';\nand they lived at the bottom of a well.'
'\n'
'...'
'...'
'\n'
None
'\n'
'Hey, buddy. Want to buy a used parser?'
'\n'
'\n'
六、 搜索文檔樹
6.1 find與find_all的區別
**find:**找到第一個滿足條件的標籤就返回。說白了,就是隻會返回一個元素。
find_all:將所有滿足條件的標籤都返回,會返回很多標籤(以列表的形式)。
6.2 使用find和find_all的過濾條件
語法: find_all(name, attrs, recursive, text, **kwargs)
- 關鍵字參數:將屬性的名字作爲關鍵字參數的名字,以及屬性的值作爲關鍵字參數的值進行過濾。
- attrs參數:將屬性條件放到一個字典中,傳給attrs參數。
6.2.1 字符串過濾實例
from bs4 import BeautifulSoup
import re
html = """
<table class="tablelist" cellpadding="0" cellspacing="0">
<tbody>
<tr class="h" >
<td class="l" width="374">職位名稱</td>
<td>職位類別</td>
<td>人數</td>
<td>地點</td>
<td>發佈時間</td>
<td href='http://www.baidu.com>網址</td>
</tr>
<p>我是一個P標籤</p>
<p id="head">我是id爲head的p標籤</p>
</tbody>
</table>
"""
soup = BeautifulSoup(html,'lxml')
# 1. 字符串過濾
trs = soup.find_all('td')
for tr in trs:
print(tr)
6.2.2 正則表達式過濾
# 正則表達式過濾: 如果傳入的是正則表達式,那麼BeautifulSoup4會通過search()來匹配內容
trs = soup.find_all(re.compile('td'))
for i in trs:
print(i)
6.2.3 列表過濾
# 列表: 如果傳入一個列表,BeautifulSoup4將會與列表中的任一元素匹配到的節點返回
t_list = soup.find_all(["td", "p"])
for item in t_list:
print(item)
6.2.4 kwargs參數
# # 查詢id=head的Tag
t_list = soup.find_all(id="head")
# # 查詢所有包含class的Tag(注意:class在Python中屬於關鍵字,所以加_以示區別)
t_list = soup.find_all(class_="h")
print(t_list)
七、select方法
使用以上方法可以方便的找出元素。但有時候使用css
選擇器的方式可以更加的方便。使用css
選擇器的語法,應該使用select
方法。以下列出幾種常用的css
選擇器方法:
7.1通過標籤名查找:
print(soup.select('a'))
7.2 通過類名查找:
通過類名,則應該在類的前面加一個.
。比如要查找class=sister
的標籤。示例代碼如下:
print(soup.select('.sister'))
7.3 通過id查找:
通過id查找,應該在id的名字前面加一個#號。示例代碼如下:
print(soup.select("#link1"))
7.4 組合查找:
組合查找即和寫 class 文件時,標籤名與類名、id名進行的組合原理是一樣的,例如查找 p 標籤中,id 等於 link1的內容,二者需要用空格分開:
print(soup.select("p #link1"))
直接子標籤查找,則使用 > 分隔:
print(soup.select("head > title"))
7.5 通過屬性查找:
查找時還可以加入屬性元素,屬性需要用中括號括起來,注意屬性和標籤屬於同一節點,所以中間不能加空格,否則會無法匹配到。示例代碼如下:
print(soup.select('a[href="http://example.com/elsie"]'))
7.7 獲取內容
以上的 select 方法返回的結果都是列表形式,可以遍歷形式輸出,然後用 get_text() 方法來獲取它的內容。
soup = BeautifulSoup(html, 'lxml')
print(type(soup.select('title')))
print(soup.select('title')[0].get_text())
for title in soup.select('title'):
print(title.get_text())