最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python BeautifulSoup高效解析網(wǎng)頁(yè)數(shù)據(jù)

 更新時(shí)間:2025年08月04日 09:53:36   作者:hui函數(shù)  
BeautifulSoup,和lxml一樣,是一個(gè)html的解析器,主要功能也是解析和提取數(shù)據(jù),本文主要來(lái)講講BeautifulSoup高效解析網(wǎng)頁(yè)數(shù)據(jù)的相關(guān)知識(shí),感興趣的小伙伴可以了解下

基本簡(jiǎn)介

在使用lxml庫(kù)解析網(wǎng)頁(yè)數(shù)據(jù)時(shí),每次都需要編寫(xiě)和測(cè)試XPath的路徑表達(dá)式,顯得非常煩瑣。為了解決這個(gè)問(wèn)題,Python還提供了Beautiful Soup提取HTML或XML文檔的節(jié)點(diǎn),Beautiful Soup使用起來(lái)更加便捷,受到了開(kāi)發(fā)人員的推崇。

BeautifulSoup簡(jiǎn)稱(chēng)

簡(jiǎn)稱(chēng):bs4

什么是BeatifulSoup

BeautifulSoup,和lxml一樣,是一個(gè)html的解析器,主要功能也是解析和提取數(shù)據(jù)

優(yōu)缺點(diǎn)

  • 缺點(diǎn):效率沒(méi)有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)打開(kāi)文件的編碼格式gbk所以需要指定打開(kāi)編碼格式

節(jié)點(diǎn)定位

根據(jù)標(biāo)簽名查找節(jié)點(diǎn)

soup.a 【注】只能找到第一個(gè)a

  • soup.a.name
  • soup.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">人生苦短 我愛(ài)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" ">人生苦短 我愛(ài)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" ">人生苦短 我愛(ài)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è)下劃線(xiàn)
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" ">人生苦短 我愛(ài)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" ">人生苦短 我愛(ài)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" ">人生苦短 我愛(ài)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)簽名稱(chēng)'):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')
?
"""普通類(lèi)型"""
# 獲取所有的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" ">人生苦短 我愛(ài)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é)果也是按照源碼格式輸出
?
"""屬性選擇器類(lèi)型"""
# 獲取有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í)別選擇器類(lèi)型"""
# 獲取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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python 多種日期時(shí)間處理函數(shù)實(shí)例詳解

    python 多種日期時(shí)間處理函數(shù)實(shí)例詳解

    Python提供了豐富的日期和時(shí)間處理函數(shù),可以幫助你輕松地解析、格式化、計(jì)算和操作日期和時(shí)間,在實(shí)際應(yīng)用中,根據(jù)具體需求選擇合適的函數(shù),可以提高工作效率并簡(jiǎn)化代碼,本文給大家介紹python多種日期時(shí)間處理函數(shù)介紹,感興趣的朋友一起看看吧
    2024-03-03
  • Python完成哈夫曼樹(shù)編碼過(guò)程及原理詳解

    Python完成哈夫曼樹(shù)編碼過(guò)程及原理詳解

    這篇文章主要介紹了Python完成哈夫曼樹(shù)編碼過(guò)程及原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python中uuid模塊實(shí)例淺析

    python中uuid模塊實(shí)例淺析

    在本篇文章里小編給大家整理了一篇關(guān)于python中uuid模塊實(shí)例淺析內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-12-12
  • python使用?toml的實(shí)現(xiàn)

    python使用?toml的實(shí)現(xiàn)

    本文將結(jié)合實(shí)例代碼介紹python使用?toml的實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • 詳解Python如何利用裝飾器實(shí)現(xiàn)重試機(jī)制

    詳解Python如何利用裝飾器實(shí)現(xiàn)重試機(jī)制

    重試機(jī)制在編程中是比較常見(jiàn)的場(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)示例

    這篇文章主要為大家介紹了深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • 值得收藏,Python 開(kāi)發(fā)中的高級(jí)技巧

    值得收藏,Python 開(kāi)發(fā)中的高級(jí)技巧

    這篇文章主要介紹了Python 開(kāi)發(fā)中的高級(jí)技巧,非常不錯(cuò),具有收藏價(jià)值,感興趣的朋友一起看看吧
    2018-11-11
  • Python實(shí)現(xiàn)大文件排序的方法

    Python實(shí)現(xiàn)大文件排序的方法

    這篇文章主要介紹了Python大文件排序的方法,涉及Python針對(duì)文件、緩存及日期等操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • 使用Python NumPy庫(kù)繪制漸變圖案

    使用Python NumPy庫(kù)繪制漸變圖案

    NumPy(Numerical Python)是Python的一種開(kāi)源的數(shù)值計(jì)算擴(kuò)展。這種工具可用來(lái)存儲(chǔ)和處理大型矩陣。但其實(shí)NumPy還可以繪制圖畫(huà),本文將為大家介紹如何通過(guò)NumPy繪制彩色圖畫(huà),感興趣的小伙伴可以了解一下
    2021-12-12
  • python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)(方法詳解)

    python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)(方法詳解)

    這篇文章主要介紹了python更新數(shù)據(jù)庫(kù)中某個(gè)字段的數(shù)據(jù)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

秭归县| 石楼县| 台东市| 蒙自县| 察隅县| 武邑县| 秦安县| 屯留县| 额敏县| 定兴县| 鄂托克旗| 郓城县| 浦北县| 镇康县| 达日县| 辽宁省| 商都县| 汝阳县| 浦江县| 苏尼特左旗| 双牌县| 兰考县| 新兴县| 望江县| 东城区| 常德市| 泰和县| 紫金县| 重庆市| 泽库县| 遂溪县| 云浮市| 富裕县| 仁怀市| 志丹县| 闸北区| 页游| 全州县| 霍山县| 阿拉善右旗| 桃源县|