Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解
BeautifulSoup 是什么
Beautiful Soup 是一個(gè)可以從 HTML 或 XML 文件中提取數(shù)據(jù)的 Python 庫(kù)。,最主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù)。能夠通過(guò)自己喜歡的轉(zhuǎn)換器實(shí)現(xiàn)慣用的文檔導(dǎo)航,查找,修改文檔的方式。
注:BeautifulSoup3目前已經(jīng)停止開(kāi)發(fā),官網(wǎng)推薦在現(xiàn)在的項(xiàng)目中使用BeautifulSoup4
bs4的安裝
可以在 Pycharm 中,輸入以下語(yǔ)句:然后可根據(jù)提示進(jìn)行安裝。
from bs4 import BeautifulSoup
注意:bs4 是依賴(lài) lxml 庫(kù)的,只有先安裝 lxml 庫(kù)才可以安裝bs4庫(kù)*
文檔解析器優(yōu)缺點(diǎn)

推薦使用 lxml 作為解析器,因?yàn)樾矢摺?/p>
bs4 的使用
- 導(dǎo)入解析包;
- 創(chuàng)建 beautifulsoup 解析對(duì)象;
- 打印對(duì)應(yīng)內(nèi)容即可;
代碼實(shí)例:
from bs4 import BeautifulSoup html_doc = """ <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" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ # 創(chuàng)建一個(gè) soup 對(duì)象 soup = BeautifulSoup(html_doc, 'lxml') print(soup, type(soup)) # <class 'bs4.BeautifulSoup'> # 格式化文檔輸出 print(soup.prettify()) # 獲取 title 標(biāo)簽的名稱(chēng) title print(soup.title.name) # title # 獲取 title 標(biāo)簽內(nèi)容 print(soup.title) # <title>The Dormouse's story</title> # title 標(biāo)簽里面的文本內(nèi)容 print(soup.title.string) # 獲取 p 段落 print(soup.p)
bs4的對(duì)象種類(lèi)
- tag : html中的標(biāo)簽??梢酝ㄟ^(guò) BeautifulSoup 分析 Tag 的具體內(nèi)容,具體格式為:soup.name,其中 name 是html 下的標(biāo)簽。
- NavigableString : 標(biāo)簽中的文本對(duì)象。
- BeautifulSoup : 整個(gè)html文本對(duì)象,可以作為T(mén)ag對(duì)象。
- Comment:特殊的 NavigableString 對(duì)象,如果 html標(biāo)簽中有注釋?zhuān)瑒t可過(guò)濾注釋符號(hào)并保留注釋文本。
代碼實(shí)例
from bs4 import BeautifulSoup html_doc = """ <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" id="link1">Elsie</a>, <a class="sister" id="link2">Lacie</a> and <a class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ """ tag:標(biāo)簽 NavigableString:可導(dǎo)航的字符串,標(biāo)簽中的文本對(duì)象 beautifulSoup:bs對(duì)象,整個(gè) html 文本對(duì)象 Comment:注釋?zhuān)绻?html 標(biāo)簽中有注釋?zhuān)瑒t可過(guò)濾注釋符號(hào)并保留注釋文本 """ # html_doc 表示要解析的文檔,而 html.parser 表示解析文檔時(shí)所用的解析器 soup = BeautifulSoup(html_doc, 'html.parser') print(soup) """ tag:標(biāo)簽""" print(type(soup.title)) print(type(soup.p)) print(type(soup.a)) """ NavigableString,可導(dǎo)航的字符串""" from bs4.element import NavigableString print(type(soup.title.string)) # 標(biāo)簽下的文本數(shù)據(jù) """beautifulSoup,bs對(duì)象""" print(type(soup)) """ Comment:注釋""" html = "<b><!--好好學(xué)習(xí),天天向上--></b>" soup2 = BeautifulSoup(html, 'html.parser') print(soup2.b.string, type(soup2.b.string))
遍歷文檔樹(shù)
遍歷子節(jié)點(diǎn)
- contents 返回的是一個(gè)所有子節(jié)點(diǎn)的列表(了解)
- children 返回的是一個(gè)子節(jié)點(diǎn)的迭代器(了解)
- descendants 返回的是一個(gè)生成器遍歷子子孫孫(了解)
- string 獲取標(biāo)簽里面的內(nèi)容(掌握)
- strings 返回是一個(gè)生成器對(duì)象用過(guò)來(lái)獲取多個(gè)標(biāo)簽內(nèi)容(掌握)
- stripped_strings 和strings 基本一致 但是它可以把多余的空格去掉(掌握)
遍歷父節(jié)點(diǎn)
- parent 直接獲得父節(jié)點(diǎn)
- parents 獲取所有的父節(jié)點(diǎn)
遍歷兄弟節(jié)點(diǎn)
- next_sibling,下一個(gè)兄弟結(jié)點(diǎn)
- previous_sibling,上一個(gè)兄弟結(jié)點(diǎn)
- next_siblings,下一個(gè)所有兄弟結(jié)點(diǎn)
- previous_siblings,上一個(gè)所有兄弟結(jié)點(diǎn)
代碼實(shí)例
from bs4 import BeautifulSoup
html_doc = """
<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" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
</body>
</html>
"""
soup = BeautifulSoup(html_doc, 'lxml')
r1 = soup.title.string # 獲取單個(gè)標(biāo)簽里面的內(nèi)容 The Dormouse's story
# 獲取html中所有的標(biāo)簽中的內(nèi)容
r2 = soup.html.strings # 返回是一個(gè)生成 generator對(duì)象,用過(guò)來(lái)獲取多個(gè)標(biāo)簽內(nèi)容
for i in r2:
print(i)
r3 = soup.html.stripped_strings # 獲取html中所有的標(biāo)簽中的內(nèi)容,并去掉多余的空格
for i in r3:
print("---", i)搜索文檔樹(shù)
- find():返回搜索到的第一條數(shù)據(jù);
- find_all():以列表形式返回所有的搜索到的標(biāo)簽數(shù)據(jù);
代碼實(shí)例
from bs4 import BeautifulSoup
html = """
<table class="tablelist" cellpadding="0" cellspacing="0">
<tbody>
<tr class="h">
<td class="l" width="374">職位名稱(chēng)</td>
<td>職位類(lèi)別</td>
<td>人數(shù)</td>
<td>地點(diǎn)</td>
<td>發(fā)布時(shí)間</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=33824&keywords=python&tid=87&lid=2218">22989-金融云區(qū)塊鏈高級(jí)研發(fā)工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=29938&keywords=python&tid=87&lid=2218">22989-金融云高級(jí)后臺(tái)開(kāi)發(fā)</a></td>
<td>技術(shù)類(lèi)</td>
<td>2</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=31236&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂(lè)運(yùn)營(yíng)開(kāi)發(fā)工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>2</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=31235&keywords=python&tid=87&lid=2218">SNG16-騰訊音樂(lè)業(yè)務(wù)運(yùn)維工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-25</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=34531&keywords=python&tid=87&lid=2218">TEG03-高級(jí)研發(fā)工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=34532&keywords=python&tid=87&lid=2218">TEG03-高級(jí)圖像算法研發(fā)工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=31648&keywords=python&tid=87&lid=2218">TEG11-高級(jí)AI開(kāi)發(fā)工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>4</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
<tr class="odd">
<td class="l square"><a target="_blank" href="position_detail.php?id=32218&keywords=python&tid=87&lid=2218">15851-后臺(tái)開(kāi)發(fā)工程師</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
<tr class="even">
<td class="l square"><a target="_blank" href="position_detail.php?id=32217&keywords=python&tid=87&lid=2218">15851-后臺(tái)開(kāi)發(fā)工程師</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
<tr class="odd">
<td class="l square"><a id="test" class="test" target='_blank' href="position_detail.php?id=34511&keywords=python&tid=87&lid=2218">SNG11-高級(jí)業(yè)務(wù)運(yùn)維工程師(深圳)</a></td>
<td>技術(shù)類(lèi)</td>
<td>1</td>
<td>深圳</td>
<td>2017-11-24</td>
</tr>
</tbody>
</table>
"""
soup = BeautifulSoup(html, 'lxml')
print(soup, type(soup))
# 獲取所有的 tr 標(biāo)簽
trs = soup.find_all("tr")
for tr in trs:
print(tr)
print('*' * 30)
# 獲取第二個(gè) tr 標(biāo)簽
tr = soup.find_all('tr')[1]
print(tr)
# 排除第一個(gè)tr值(通過(guò)切片的方式)
jobMsg = soup.find_all('tr')[1:]
print(jobMsg)
# 獲取所有的 class = even 的 tr 標(biāo)簽:
# trs = soup.find_all('tr', class_='even') # class為關(guān)鍵字,不能直接用作變量名
trs = soup.find_all('tr', attrs={"class": "even"}) # 效果同上,如果有多個(gè)值,在后面添加即可,推薦
for tr in trs:
print(tr)
print('--' * 44)
# 獲取所有a標(biāo)簽里面的 href 屬性值:
allA = soup.find_all('a')
for a in allA:
href = a.get('href')
print(href)
# 獲取所有的崗位信息
trs = soup.find_all('tr')[1:] # 把第一個(gè)的表頭去掉
# print(trs)
for tr in trs:
tds = tr.find_all('td') # 找到所有的 td
jobName = tds[0].string # 獲取文本數(shù)據(jù),如果數(shù)據(jù)狠多,可以使用strings
print(jobName)以上就是Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python BeautifulSoup4的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python解析庫(kù)Beautiful?Soup安裝的詳細(xì)步驟
- Python爬蟲(chóng)之使用BeautifulSoup和Requests抓取網(wǎng)頁(yè)數(shù)據(jù)
- Python Beautiful Soup模塊使用教程詳解
- Python BeautifulSoup基本用法詳解(通過(guò)標(biāo)簽及class定位元素)
- python BeautifulSoup庫(kù)的安裝與使用
- Python中BeautifulSoup通過(guò)查找Id獲取元素信息
- Python基于BeautifulSoup爬取京東商品信息
- Python Beautiful Soup 使用示例詳解
相關(guān)文章
NumPy實(shí)現(xiàn)多維數(shù)組中的線性代數(shù)
本文主要介紹了NumPy實(shí)現(xiàn)多維數(shù)組中的線性代數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Python通過(guò)4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信
這篇文章主要介紹了Python通過(guò)4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文
這篇文章主要介紹了Python實(shí)現(xiàn)把數(shù)字轉(zhuǎn)換成中文,一般用于數(shù)字金額轉(zhuǎn)中文大寫(xiě)金額,即將阿拉伯?dāng)?shù)字轉(zhuǎn)換為大寫(xiě)的中文,需要的朋友可以參考下2015-06-06
python 讀取txt中每行數(shù)據(jù),并且保存到excel中的實(shí)例
下面小編就為大家分享一篇python 讀取txt中每行數(shù)據(jù),并且保存到excel中的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python3操作讀寫(xiě)CSV文件使用包過(guò)程解析
這篇文章主要介紹了Python3操作CSV文件使用包過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題
今天小編就為大家分享一篇解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
基于Python3讀寫(xiě)INI配置文件過(guò)程解析
這篇文章主要介紹了基于Python3讀寫(xiě)INI配置文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Python 語(yǔ)法錯(cuò)誤:"SyntaxError: invalid charac
本文給大家分享Python 語(yǔ)法錯(cuò)誤:“SyntaxError: invalid character in identifier“,原因及解決方法,文末給大家補(bǔ)充介紹了Python出現(xiàn)SyntaxError: invalid syntax的原因總結(jié),感興趣的朋友跟隨小編一起學(xué)習(xí)吧2023-02-02
python實(shí)現(xiàn)TCPclient的使用示例
python實(shí)現(xiàn)TCPclient是一件簡(jiǎn)單的事情,只要通過(guò)socket這個(gè)模塊就可以實(shí)現(xiàn),本文主要介紹了python實(shí)現(xiàn)TCPclient的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10

