實業(yè)+東莞網(wǎng)站建設網(wǎng)絡seo推廣
BeautifulSoup 是一個可以從 HTML 或 XML 文件中提取數(shù)據(jù)的 Python 庫,它能夠?qū)?HTML 或 XML 轉(zhuǎn)化為可定位的樹形結構,并提供了導航、查找、修改功能,它會自動將輸入文檔轉(zhuǎn)換為 Unicode 編碼,輸出文檔轉(zhuǎn)換為 UTF-8 編碼。
BeautifulSoup 支持 Python 標準庫中的 HTML 解析器和一些第三方的解析器,默認使用 Python 標準庫中的 HTML 解析器,默認解析器效率相對比較低,如果需要解析的數(shù)據(jù)量比較大或比較頻繁,推薦使用更強、更快的 lxml 解析器。
1 安裝
1)BeautifulSoup 安裝
如果使用 Debain 或 ubuntu 系統(tǒng),可以通過系統(tǒng)的軟件包管理來安裝:apt-get install Python-bs4
,如果無法使用系統(tǒng)包管理安裝,可以使用?pip install beautifulsoup4
?來安裝。
2)第三方解析器安裝
如果需要使用第三方解釋器 lxml 或 html5lib,可是使用如下命令進行安裝:apt-get install Python-lxml(html5lib)
?和?pip install lxml(html5lib)
。
看一下主要解析器和它們的優(yōu)缺點:
解析器 | 使用方法 | 優(yōu)勢 | 劣勢 |
---|---|---|---|
Python標準庫 | BeautifulSoup(markup,"html.parser") | Python的內(nèi)置標準庫;執(zhí)行速度適中;文檔容錯能力強。 | Python 2.7.3 or 3.2.2)前的版本中文檔容錯能力差。 |
lxml HTML 解析器 | BeautifulSoup(markup,"lxml") | 速度快;文檔容錯能力強。 | 需要安裝C語言庫。 |
lxml XML 解析器 |
| 速度快;唯一支持XML的解析器。 | 需要安裝C語言庫 |
html5lib | BeautifulSoup(markup,"html5lib") | 最好的容錯性;以瀏覽器的方式解析文檔;生成HTML5格式的文檔。 | 速度慢;不依賴外部擴展。 |
2 快速上手
將一段文檔傳入 BeautifulSoup 的構造方法,就能得到一個文檔的對象,可以傳入一段字符串或一個文件句柄,示例如下:
1)使用字符串
我們以如下一段 HTML 字符串為例:
html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BeautifulSoup學習</title>
</head>
<body>
Hello BeautifulSoup
</body>
</html>
'''
使用示例如下:
from bs4 import BeautifulSoup
#使用默認解析器
soup = BeautifulSoup(html,'html.parser')
#使用 lxml 解析器
soup = BeautifulSoup(html,'lxml')
2)本地文件
還以上面那段 HTML 為例,將上面 HTML 字符串放在 index.html 文件中,使用示例如下:
???????
#使用默認解析器
soup = BeautifulSoup(open('index.html'),'html.parser')
#使用 lxml 解析器
soup = BeautifulSoup(open('index.html'),'lxml')
2.1 對象的種類
BeautifulSoup 將 HTML 文檔轉(zhuǎn)換成一個樹形結構,每個節(jié)點都是 Python 對象,所有對象可以歸納為4種:Tag
,NavigableString
,BeautifulSoup
,Comment
。
1)Tag 對象
Tag 對象與 HTML 或 XML 原生文檔中的 tag 相同,示例如下:
???????
soup = BeautifulSoup('<title>BeautifulSoup學習</title>','lxml')
tag = soup.title
tp =type(tag)
print(tag)
print(tp)
#輸出結果
'''
<title>BeautifulSoup學習</title>
<class 'bs4.element.Tag'>
'''
Tag 有很多方法和屬性,這里先看一下它的的兩種常用屬性:name
?和?attributes
。
我們可以通過?.name
?來獲取 tag 的名字,示例如下:
???????
soup = BeautifulSoup('<title>BeautifulSoup學習</title>','lxml')
tag = soup.title
print(tag.name)
#輸出結果
#title
我們還可以修改 tag 的 name,示例如下:
???????
tag.name = 'title1'
print(tag)
#輸出結果
#<title1>BeautifulSoup學習</title1>
一個 tag 可能有很多個屬性,先看一它的?class
?屬性,其屬性的操作方法與字典相同,示例如下:
???????
soup = BeautifulSoup('<title class="tl">BeautifulSoup學習</title>','lxml')
tag = soup.title
cls = tag['class']
print(cls)
#輸出結果
#['tl']
我們還可以使用?.attrs
?來獲取,示例如下:
???????
ats = tag.attrs
print(ats)
#輸出結果
#{'class': ['tl']}
tag 的屬性可以被添加、修改和刪除,示例如下:
???????
#添加 id 屬性
tag['id'] = 1
#修改 class 屬性
tag['class'] = 'tl1'
#刪除 class 屬性
del tag['class']
2)NavigableString 對象
NavigableString 類是用來包裝 tag 中的字符串內(nèi)容的,使用?.string
?來獲取字符串內(nèi)容,示例如下:
str = tag.string
可以使用?replace_with()
?方法將原有字符串內(nèi)容替換成其它內(nèi)容 ,示例如下:
tag.string.replace_with('BeautifulSoup')
3)BeautifulSoup 對象
BeautifulSoup 對象表示的是一個文檔的全部內(nèi)容,它并不是真正的 HTML 或 XML 的 tag,因此它沒有?name
?和?attribute
?屬性,為方便查看它的?name
?屬性,BeautifulSoup 對象包含了一個值為?[document]
?的特殊屬性?.name
,示例如下:
???????
soup = BeautifulSoup('<title class="tl">BeautifulSoup學習</title>','lxml')
print(soup.name)
#輸出結果
#[document]
4)Comment 對象
Comment 對象是一個特殊類型的 NavigableString 對象,它會使用特殊的格式輸出,看一下例子:
???????
soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
comment = soup.title.prettify()
print(comment)
#輸出結果
'''
<title class="tl">
Hello BeautifulSoup
</title>
'''
我們前面看的例子中 tag 中的字符串內(nèi)容都不是注釋內(nèi)容,現(xiàn)在將字符串內(nèi)容換成注釋內(nèi)容,我們來看一下效果:
???????
soup = BeautifulSoup('<title class="tl"><!--Hello BeautifulSoup--></title>','html.parser')
str = soup.title.string
print(str)
#輸出結果
#Hello BeautifulSoup
通過結果我們發(fā)現(xiàn)注釋符號?<!---->
?被自動去除了,這一點我們要注意一下。
2.2 搜索文檔樹
BeautifulSoup 定義了很多搜索方法,我們來具體看一下。
1)find_all()
find_all() 方法搜索當前 tag 的所有 tag 子節(jié)點,方法詳細如下:find_all(name=None, attrs={}, recursive=True, text=None,limit=None, **kwargs)
,來具體看一下各個參數(shù)。
name
?參數(shù)可以查找所有名字為?name
?的 tag,字符串對象會被自動忽略掉,示例如下:
???????
soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
print(soup.find_all('title'))
#輸出結果
#[<title class="tl">Hello BeautifulSoup</title>]
attrs
?參數(shù)定義一個字典參數(shù)來搜索包含特殊屬性的 tag,示例如下:
???????
soup = BeautifulSoup('<title class="tl">Hello BeautifulSoup</title>','html.parser')
soup.find_all(attrs={"class": "tl"})
調(diào)用 find_all() 方法時,默認會檢索當前 tag 的所有子孫節(jié)點,通過設置參數(shù)?recursive=False
,可以只搜索 tag 的直接子節(jié)點,示例如下:
???????
soup = BeautifulSoup('<html><head><title>Hello BeautifulSoup</title></head></html>','html.parser')
print(soup.find_all('title',recursive=False))
#輸出結果
#[]
通過?text
?參數(shù)可以搜搜文檔中的字符串內(nèi)容,它接受字符串、正則表達式、列表、True,示例如下:
???????
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup('<head>myHead</head><title>BeautifulSoup</title>','html.parser')
#字符串
soup.find_all(text='BeautifulSoup')
#正則表達式
soup.find_all(soup.find_all(text=re.compile('title')))
#列表
soup.find_all(soup.find_all(text=['head','title']))
#True
soup.find_all(text=True)
limit
?參數(shù)與 SQL 中的?limit
?關鍵字類似,用來限制搜索的數(shù)據(jù),示例如下:
???????
soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
soup.find_all('a', limit=1)
我們經(jīng)常見到 Python 中?*arg
?和?**kwargs
?這兩種可變參數(shù),*arg
?表示非鍵值對的可變數(shù)量的參數(shù),將參數(shù)打包為 tuple 傳遞給函數(shù);**kwargs
?表示關鍵字參數(shù),參數(shù)是鍵值對形式的,將參數(shù)打包為 dict 傳遞給函數(shù)。
使用多個指定名字的參數(shù)可以同時過濾 tag 的多個屬性,如:
???????
soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
soup.find_all(href=re.compile("elsie"),id='link1')
有些 tag 屬性在搜索不能使用,如 HTML5 中的?data-*
?屬性,示例如下:
???????
soup = BeautifulSoup('<div data-foo="value">foo!</div>')
soup.find_all(data-foo='value')
首先當我在 Pycharm 中輸入?data-foo='value'
?便提示語法錯誤了,然后我不管提示直接執(zhí)行提示?SyntaxError: keyword can't be an expression
?這個結果也驗證了?data-*
?屬性在搜索中不能使用。我們可以通過 find_all() 方法的?attrs
?參數(shù)定義一個字典參數(shù)來搜索包含特殊屬性的 tag,示例如下:
print(soup.find_all(attrs={'data-foo': 'value'}))
2)find()
方法詳細如下:find(name=None, attrs={}, recursive=True, text=None,**kwargs)
,我們可以看出除了少了?limit
?參數(shù),其它參數(shù)與方法?find_all
?一樣,不同之處在于:find_all() 方法的返回結果是一個列表,find() 方法返回的是第一個節(jié)點,find_all() 方法沒有找到目標是返回空列表,find() 方法找不到目標時,返回 None。來看個例子:
???????
soup = BeautifulSoup('<a id="link1" href="http://example.com/elsie">Elsie</a><a id="link2" href="http://example.com/elsie">Elsie</a>','html.parser')
print(soup.find_all('a', limit=1))
print(soup.find('a'))
#輸出結果
'''
[<a href="http://example.com/elsie" id="link1">Elsie</a>]
<a href="http://example.com/elsie" id="link1">Elsie</a>
'''
從示例中我們也可以看出,find() 方法返回的是找到的第一個節(jié)點。
3)find_parents() 和 find_parent()
find_all() 和 find() 用來搜索當前節(jié)點的所有子節(jié)點,find_parents() 和 find_parent() 則用來搜索當前節(jié)點的父輩節(jié)點。
4)find_next_siblings() 和 find_next_sibling()
這兩個方法通過 .next_siblings 屬性對當前 tag 所有后面解析的兄弟 tag 節(jié)點進行迭代,find_next_siblings() 方法返回所有符合條件的后面的兄弟節(jié)點,find_next_sibling() 只返回符合條件的后面的第一個tag節(jié)點。
5)find_previous_siblings() 和 find_previous_sibling()
這兩個方法通過 .previous_siblings 屬性對當前 tag 前面解析的兄弟 tag 節(jié)點進行迭代,find_previous_siblings() 方法返回所有符合條件的前面的兄弟節(jié)點,find_previous_sibling() 方法返回第一個符合條件的前面的兄弟節(jié)點。
6)find_all_next() 和 find_next()
這兩個方法通過 .next_elements 屬性對當前 tag 之后的 tag 和字符串進行迭代,find_all_next() 方法返回所有符合條件的節(jié)點,find_next() 方法返回第一個符合條件的節(jié)點。
7)find_all_previous() 和 find_previous()
這兩個方法通過 .previous_elements 屬性對當前節(jié)點前面的 tag 和字符串進行迭代,find_all_previous() 方法返回所有符合條件的節(jié)點,find_previous() 方法返回第一個符合條件的節(jié)點。
2.3 CSS選擇器
BeautifulSoup 支持大部分的 CSS 選擇器,在 Tag 或 BeautifulSoup 對象的 .select() 方法中傳入字符串參數(shù),即可使用 CSS 選擇器的語法找到 tag,返回類型為列表。示例如下:
???????
soup = BeautifulSoup('<body><a id="link1" class="elsie">Elsie</a><a id="link2" class="elsie">Elsie</a></body>','html.parser')
print(soup.select('a'))
#輸出結果
#[<a clss="elsie" id="link1">Elsie</a>, <a clss="elsie" id="link2">Elsie</a>]
通過標簽逐層查找
soup.select('body a')
找到某個 tag 標簽下的直接子標簽
soup.select('body > a')
通過類名查找
???????
soup.select('.elsie')
soup.select('[class~=elsie]')
通過 id 查找
soup.select('#link1')
使用多個選擇器
soup.select('#link1,#link2')
通過屬性查找
soup.select('a[class]')
通過屬性的值來查找
soup.select('a[class="elsie"]')
查找元素的第一個
soup.select_one('.elsie')
查找兄弟節(jié)點標簽
??????????????
#查找所有
soup.select('#link1 ~ .elsie')
#查找第一個
soup.select('#link1 + .elsie')