通過前面網頁下載器得到一個網頁源代碼的很長的字符串,接下來則是要通過網頁解析器對網頁源代碼中的信息進行提取,beautifulsoup4 庫作為第三方插件同時支持 html、xml 的解析。通過將網頁下載器下載的 html 字符串解析成為一個 BeautifulSoup 的對象,最後從這個對象中根據網頁源代碼的 html 標籤、屬性等因素提取我們需要的內容。
1、準備網頁下載器獲取的源代碼
1# 首先獲取到網頁下載器已經下載到的網頁源代碼
2# 這裏直接取官方的案例
3html_doc = """
4<html><head><title>The Dormouse's story</title></head>
5<body>
6<p class="title"><b>The Dormouse's story</b></p>
7
8<p class="story">Once upon a time there were three little sisters; and their names were
9<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
10<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
11<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
12and they lived at the bottom of a well.</p>
13
14<p class="story">...</p>
15"""
2、導入 beautifulsoup4 庫並創建解析對象
1# 導入 beautifulsoup4 庫、用於完成解析
2from bs4 import BeautifulSoup
3
4'''
5創建 BeautifulSoup 對象、html_doc 為執行要解析的字符串、html.parser 為指定的解析器,
6除此之外,還有其他的解析庫,比如 htm5llib、lxml,各個解析庫各有優勢
7'''
8beau_soup = BeautifulSoup(html_doc, 'html.parser')
3、使用結構化的方式獲取元素、屬性等
1'''
2獲取結構化元素或屬性
3'''
4# 獲取 title 元素、也就是 title 標籤
5print beau_soup.title
6# <title>The Dormouse's story</title>
7
8# 獲取第一個 p 元素
9print beau_soup.p
10# <p class="title"><b>The Dormouse's story</b></p>
11
12# 獲取第一個 p 元素的 class 屬性
13print beau_soup.p['class']
14# [u'title']
15
16# 獲取第一個 p 元素下面的 b 元素
17print beau_soup.p.b
18# <b>The Dormouse's story</b>
19
20# 獲取 p 元素的父節點的源代碼
21print beau_soup.p.parent
22'''
23<body>
24<p class="title"><b>The Dormouse's story</b></p>
25<p class="story">Once upon a time there were three little sisters; and their names were
26<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
27<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and
28<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>;
29and they lived at the bottom of a well.</p>
30<p class="story">...</p>
31</body>
32'''
33# 獲取 p 元素的父節點的名稱
34print beau_soup.p.parent.name
35# body
4、通過元素搜索的方式獲取元素、屬性等
1'''
2除了通過結構化的方式獲取元素,在其他情況使用結構化不容易獲取元素時,
3可以使用類似於的搜索的功能對源代碼的標籤、屬性等進行篩選。
4find() 函數、find_all() 函數可以利用多個條件的模式對源代碼標籤等
5進行搜索。
6'''
7'''
8find_all(self, name=None, attrs={}, recursive=True, text=None,
9 limit=None, **kwargs)
10結果返回一個 list 集合
11'''
12
13# 搜索所有 p 元素、然後返回一個 p 元素的 list
14print beau_soup.find_all('p')
15# 搜索所有 a 元素、然後返回一個 a 元素的 list
16links = beau_soup.find_all('a')
17for link in links:
18 print '未爬取的鏈接:',link['href']
19
20# 多條件查找,獲取 p 元素、並且 class 屬性 == title 的元素
21print beau_soup.find_all('p',class_='title')
22'''
23 find(self, name=None, attrs={}, recursive=True, text=None,
24 **kwargs)
25結果只返回一個,如果有多個則返回第一個,相比 find_all() 函數少了 limit 參數
26'''
27
28# 通過 id 搜索
29print beau_soup.find(id='link3')
30
31# 多條件查找,獲取 p 元素、並且 class 屬性 == title 的元素
32print beau_soup.find('p',class_='title')
33
34import re
35
36# 多條件查找,獲取 a 元素的 href 屬性中包含 lacie 字符串的元素對象
37print beau_soup.find('a',href=re.compile(r"lacie"))
5、通過樣式選擇器的方式獲取元素、屬性等
1'''
2除了上述使用結構化獲取、元素/屬性查找的方式,還提供了 select()
3函數通過 css 樣式選擇器的方式進行元素獲取,這個函數返回的也是一個 list
4'''
5print beau_soup.select('html head title')
6# html head title 在 css 選擇器中表示 html 標籤下面的 head 標籤下面的 title 標籤
7
8print beau_soup.select('#link3')
9# #link3 樣式選擇器中 id 為 link3 的元素
更多精彩前往微信公眾號【Python 集中營】,關注獲取《python 從入門到精通全套視頻》