Python BeautifulSoup高效解析網(wǎng)頁(yè)數(shù)據(jù)
基本簡(jiǎn)介
在使用lxml庫(kù)解析網(wǎng)頁(yè)數(shù)據(jù)時(shí),每次都需要編寫和測(cè)試XPath的路徑表達(dá)式,顯得非常煩瑣。為了解決這個(gè)問題,Python還提供了Beautiful Soup提取HTML或XML文檔的節(jié)點(diǎn),Beautiful Soup使用起來(lái)更加便捷,受到了開發(fā)人員的推崇。
BeautifulSoup簡(jiǎn)稱
簡(jiǎn)稱:bs4
什么是BeatifulSoup
BeautifulSoup,和lxml一樣,是一個(gè)html的解析器,主要功能也是解析和提取數(shù)據(jù)
優(yōu)缺點(diǎn)
- 缺點(diǎn):效率沒有l(wèi)xml的效率高
- 優(yōu)點(diǎn):接口設(shè)計(jì)人性化,使用方便
安裝以及創(chuàng)建
安裝
pip install bs4
導(dǎo)入
from bs4 import BeautifulSoup
創(chuàng)建對(duì)象
服務(wù)器響應(yīng)的文件生成對(duì)象
soup = BeautifulSoup(response.text, 'lxml')
本地文件生成對(duì)象
soup = BeautifulSoup(open('1.html','r','encoding='utf-8'), 'lxml')注意:默認(rèn)打開文件的編碼格式gbk所以需要指定打開編碼格式
節(jié)點(diǎn)定位
根據(jù)標(biāo)簽名查找節(jié)點(diǎn)
soup.a 【注】只能找到第一個(gè)a
soup.a.namesoup.a.attrs
練習(xí)文檔html(后面的所有都是根據(jù)這個(gè)來(lái)練習(xí)):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
?
<div>
<ul>
<li id="l1">張三</li>
<li id="l2">李四</li>
<li>王五</li>
<a href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" " class="a1">人生苦短 我愛python</a>
<span>嘿嘿嘿</span>
</ul>
</div>
?
?
<a href="" title=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a2">百度</a>
?
<div id="d1">
<span>
哈哈哈
</span>
</div>
?
<p id="p1" class="p1">呵呵呵</p>
</body>
</html>代碼:
from bs4 import BeautifulSoup
# 創(chuàng)建對(duì)象
html = BeautifulSoup(open('bs4的基本使用.html','r',encoding='utf-8'),'lxml')
?
# 獲取a標(biāo)簽對(duì)象
print(html.a)
# 結(jié)果:<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>
?
# 獲取a標(biāo)簽的name
print(html.a.name)
# 結(jié)果:a
?
# 獲取a標(biāo)簽的屬性
print(html.a.attrs)
# 結(jié)果:{'href': '', 'id': '', 'class': ['a1']}函數(shù)
find(返回一個(gè)對(duì)象)
語(yǔ)法:
find('a'):只找到第一個(gè)a標(biāo)簽
find('a', title='名字')
find('a', class_='名字')代碼:
from bs4 import BeautifulSoup
# 創(chuàng)建對(duì)象
html = BeautifulSoup(open('bs4的基本使用.html','r',encoding='utf-8'),'lxml')
?
# 獲取a標(biāo)簽
res = html.find('a')
print(res)
# 結(jié)果:<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>
?
# 獲取title是a2的a標(biāo)簽
res = html.find('a',title = 'a2')
print(res)
# 結(jié)果:<a href="" title=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a2">百度</a>
?
# 獲取class為a1的a標(biāo)簽,注意由于class是關(guān)鍵字,所以后面要添加一個(gè)下劃線
res = html.find('a',class_ = 'a1')
print(res)
# 結(jié)果:<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>find_all(返回一個(gè)列表)
語(yǔ)法:
find_all('a') 查找到所有的a
find_all(['a', 'span']) 返回所有的a和span
find_all('a', limit=2) 只找前兩個(gè)a代碼:
from bs4 import BeautifulSoup
# 創(chuàng)建對(duì)象
html = BeautifulSoup(open('bs4的基本使用.html','r',encoding='utf-8'),'lxml')
?
# 獲取所有的a標(biāo)簽
res = html.find_all('a')
print(res)
# 結(jié)果:[<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>, <a href="" title=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a2">百度</a>]
?
# 獲取所有的a標(biāo)簽和span標(biāo)簽
res = html.find_all(['a','span'])
print(res)
# 結(jié)果:[<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>, <span>嘿嘿嘿</span>, <a href="" title=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a2">百度</a>, <span>
# 哈哈哈
# </span>]
# 注意:結(jié)果里面的格式和源碼一致
?
# 方法1:使用limit參數(shù)
# 獲取前兩個(gè)li標(biāo)簽
res = html.find_all('li',limit=2)
# 方法2:使用下標(biāo)索引
# res = html.find_all('li')[:2]
print(res)
# 結(jié)果:[<li id="l1">張三</li>, <li id="l2">李四</li>]select(根據(jù)選擇器得到節(jié)點(diǎn)對(duì)象)【推薦】
語(yǔ)法:
select('標(biāo)簽名稱'):select('p')
select('標(biāo)簽class'):select('.page')
select('標(biāo)簽id'):select('#page')
屬性選擇器
- select('li[屬性]'):select('li[class]')
- select('li[屬性=屬性值]'):select('li[class = "page"]')
層級(jí)選擇
- div span:select('div span')
- div > span:select('div>span')
其它
一次選擇多個(gè):select('li,span')
代碼:
from bs4 import BeautifulSoup
# 創(chuàng)建對(duì)象
html = BeautifulSoup(open('bs4的基本使用.html','r',encoding='utf-8'),'lxml')
?
"""普通類型"""
# 獲取所有的li標(biāo)簽
res = html.select('li')
print(res)
# 結(jié)果:[<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>]
?
# 獲取class為a1的標(biāo)簽
res = html.select('.a1')
print(res)
# 結(jié)果:[<a class="a1" href="" id=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ">人生苦短 我愛python</a>]
?
# 獲取id為l1的標(biāo)簽
res = html.select('#l1')
print(res)
# 結(jié)果:[<li id="l1">張三</li>]
?
# 獲取所有a標(biāo)簽和span標(biāo)簽
res = html.select('li,span')
print(res)
# 結(jié)果:
# [<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>, <span>嘿嘿嘿</span>, <span>
# 哈哈哈
# </span>]
# 注意:結(jié)果也是按照源碼格式輸出
?
"""屬性選擇器類型"""
# 獲取有title屬性的標(biāo)簽
res = html.select('[title]')
print(res)
# 結(jié)果:[<a href="" title=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a2">百度</a>]
?
# 獲取有id屬性的li標(biāo)簽
res = html.select('li[id]')
print(res)
# 結(jié)果:[<li id="l1">張三</li>, <li id="l2">李四</li>]
?
# 獲取id屬性值為l1的標(biāo)簽
res = html.select('[id = "l1"]')
print(res)
# 結(jié)果:[<li id="l1">張三</li>]
?
# 獲取li標(biāo)簽里面id屬性值為l2的標(biāo)簽
res = html.select('li[id = "l2"]')
print(res)
# 結(jié)果:[<li id="l2">李四</li>]
?
"""級(jí)別選擇器類型"""
# 獲取div里面的所有l(wèi)i標(biāo)簽
# 方法1
res = html.select('div li')
# 方法2
res = html.select('div ul li')
# 方法3
res = html.select('div>ul>li')
print(res)
# 結(jié)果:[<li id="l1">張三</li>, <li id="l2">李四</li>, <li>王五</li>]獲取節(jié)點(diǎn)信息
獲取節(jié)點(diǎn)內(nèi)容:適用于標(biāo)簽中嵌套標(biāo)簽的結(jié)構(gòu)
obj.string(有層級(jí)限制)
obj.get_text()【推薦】(無(wú)層級(jí)限制)
節(jié)點(diǎn)的屬性
tag.name 獲取標(biāo)簽名
tag.attrs 將屬性值作為一個(gè)字典返回
獲取節(jié)點(diǎn)屬性
- obj.attrs.get('title')【常用】
- obj.get('title')
- obj['title']
代碼:
from bs4 import BeautifulSoup
# 創(chuàng)建對(duì)象
html = BeautifulSoup(open('bs4的基本使用.html','r',encoding='utf-8'),'lxml')
?
"""獲取文本"""
# 獲取div的id為d1里面span標(biāo)簽的文本(有層級(jí)限制,需一個(gè)一個(gè)進(jìn)入span,非則報(bào)錯(cuò))
res = html.select('div[id = "d1"]>span')[0]
print(res.string)
# 結(jié)果:哈哈哈
?
# 獲取div的id為d1里面span標(biāo)簽的文本(無(wú)層級(jí)限制,無(wú)需一個(gè)一個(gè)進(jìn)入span)
res = html.select('div[id = "d1"]')[0]
print(res.get_text())
# 結(jié)果:哈哈哈
?
"""獲取標(biāo)簽屬性值"""
# 獲取百度標(biāo)簽里面的屬性值
res = html.select('a[title = "a2"]')[0]
# 方法1
print(res.attrs.get('href'))
# 方法2
print(res.get('href'))
# 方法3
print(res['href'])
# 結(jié)果:https://www.baidu.com到此這篇關(guān)于Python BeautifulSoup高效解析網(wǎng)頁(yè)數(shù)據(jù)的文章就介紹到這了,更多相關(guān)BeautifulSoup解析網(wǎng)頁(yè)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python使用BeautifulSoup提取網(wǎng)頁(yè)數(shù)據(jù)的完整指南
- Python使用BeautifulSoup抓取和解析網(wǎng)頁(yè)數(shù)據(jù)的操作方法
- Python利用BeautifulSoup解析網(wǎng)頁(yè)內(nèi)容
- Python使用BeautifulSoup爬取網(wǎng)頁(yè)數(shù)據(jù)的操作步驟
- python?中的?BeautifulSoup?網(wǎng)頁(yè)使用方法解析
- Python如何使用BeautifulSoup爬取網(wǎng)頁(yè)信息
- python基于BeautifulSoup實(shí)現(xiàn)抓取網(wǎng)頁(yè)指定內(nèi)容的方法
相關(guān)文章
python 多種日期時(shí)間處理函數(shù)實(shí)例詳解
Python提供了豐富的日期和時(shí)間處理函數(shù),可以幫助你輕松地解析、格式化、計(jì)算和操作日期和時(shí)間,在實(shí)際應(yīng)用中,根據(jù)具體需求選擇合適的函數(shù),可以提高工作效率并簡(jiǎn)化代碼,本文給大家介紹python多種日期時(shí)間處理函數(shù)介紹,感興趣的朋友一起看看吧2024-03-03
詳解Python如何利用裝飾器實(shí)現(xiàn)重試機(jī)制
重試機(jī)制在編程中是比較常見的場(chǎng)景,主要被用于處理那些可能由于臨時(shí)性故障或網(wǎng)絡(luò)波動(dòng)等原因而失敗的操作,下面我們就來(lái)看看如何使用裝飾器來(lái)實(shí)現(xiàn)重試機(jī)制吧2024-03-03
深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例
這篇文章主要為大家介紹了深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)(方法詳解)
這篇文章主要介紹了python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11

