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

python網(wǎng)絡(luò)爬蟲精解之Beautiful Soup的使用說(shuō)明

 更新時(shí)間:2021年09月28日 08:33:39   作者:小狐貍夢(mèng)想去童話鎮(zhèn)  
簡(jiǎn)單來(lái)說(shuō),Beautiful Soup 是 python 的一個(gè)庫(kù),最主要的功能是從網(wǎng)頁(yè)抓取數(shù)據(jù),Beautiful Soup 提供一些簡(jiǎn)單的、python 式的函數(shù)用來(lái)處理導(dǎo)航、搜索、修改分析樹等功能,需要的朋友可以參考下

一、Beautiful Soup的介紹

Beautiful Soup是一個(gè)強(qiáng)大的解析工具,它借助網(wǎng)頁(yè)結(jié)構(gòu)和屬性等特性來(lái)解析網(wǎng)頁(yè)。

它提供一些函數(shù)來(lái)處理導(dǎo)航、搜索、修改分析樹等功能,Beautiful Soup不需要考慮文檔的編碼格式。Beautiful Soup在解析時(shí)實(shí)際上需要依賴解析器,常用的解析器是lxml。

二、Beautiful Soup的使用

test03.html測(cè)試實(shí)例:

<!DOCTYPE html>
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="content-type" />
    <meta content="IE=Edge" http-equiv="X-UA-Compatible" />
    <meta content="always" name="referrer" />
    <link  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="stylesheet" type="text/css" />
    <title>百度一下,你就知道 </title>
</head>
<body link="#0000cc">
  <div id="wrapper">
    <div id="head">
        <div class="head_wrapper">
          <div id="u1">
            <a class="mnav"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_trnews">新聞 </a>
            <a class="mnav"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_trhao123">hao123 </a>
            <a class="mnav"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_trmap">地圖 </a>
            <a class="mnav"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_trvideo">視頻 </a>
            <a class="mnav"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_trtieba">貼吧 </a>
            <a class="bri"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>
          </div>
        </div>
    </div>
  </div>
</body>
</html>

1、節(jié)點(diǎn)選擇器

我們之前了解到,一個(gè)網(wǎng)頁(yè)是由若干個(gè)元素節(jié)點(diǎn)組成的,通過(guò)提取某個(gè)節(jié)點(diǎn)的具體內(nèi)容,就可以獲取到界面呈現(xiàn)的一些數(shù)據(jù)。使用節(jié)點(diǎn)選擇器能夠簡(jiǎn)化我們獲取數(shù)據(jù)的過(guò)程,在不使用正則表達(dá)式的前提下,精準(zhǔn)的獲取數(shù)據(jù)。

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.head)
print(soup.head.title)
print(soup.a)

【運(yùn)行結(jié)果】

<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type"/>
<meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
<meta content="always" name="referrer"/>
<link rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
<title>百度一下,你就知道 </title>
</head>
<title>百度一下,你就知道 </title>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>

分析:

第一條打印數(shù)據(jù)為獲取網(wǎng)頁(yè)的head節(jié)點(diǎn);

第二條打印內(nèi)容是獲取head節(jié)點(diǎn)中title節(jié)點(diǎn),獲取該節(jié)點(diǎn)使用了一個(gè)嵌套選擇,因?yàn)閠itle節(jié)點(diǎn)是嵌套在head節(jié)點(diǎn)里面的;

第三條打印內(nèi)容是獲取a節(jié)點(diǎn),在源碼中我們看到有許多條a節(jié)點(diǎn),而只匹配到第一個(gè)a節(jié)點(diǎn)就結(jié)束了。當(dāng)有多個(gè)節(jié)點(diǎn)時(shí),這種選擇方式指只會(huì)選擇第一個(gè)匹配的節(jié)點(diǎn),其他后面節(jié)點(diǎn)會(huì)忽略。

2、提取信息

一般我們需要的數(shù)據(jù)位于節(jié)點(diǎn)名、屬性值、文本值中,以下代碼展示了如何獲取這三個(gè)地方的數(shù)據(jù):

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.body.name)
print(soup.body.a.attrs['class'])
print(soup.body.a.attrs['href'])
print(soup.body.a.string)

【運(yùn)行結(jié)果】

body
['mnav']
http://news.baidu.com
新聞

分析:

第一條獲取body節(jié)點(diǎn)名;

第二條獲取a節(jié)點(diǎn)class屬性值;

第三條獲取a節(jié)點(diǎn)href屬性值;

第四條獲取a節(jié)點(diǎn)的文本值;

3、關(guān)聯(lián)選擇

(1)子節(jié)點(diǎn)和子孫節(jié)點(diǎn)

子節(jié)點(diǎn)可以調(diào)用contents屬性和children屬性,子孫節(jié)點(diǎn)可以調(diào)用descendants屬性,他們返回結(jié)果都是生成器類型,通過(guò)for循環(huán)輸出匹配到的信息。

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
# print(soup.body.contents)
for i,content in enumerate(soup.body.contents):
    print(i,content)

【運(yùn)行結(jié)果】

0

1 <div id="wrapper">
<div id="head">
<div class="head_wrapper">
<div id="u1">
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trhao123">hao123 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trmap">地圖 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trvideo">視頻 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trtieba">貼吧 </a>
<a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>
</div>
</div>
</div>
</div>
2

(2)父節(jié)點(diǎn)和祖先節(jié)點(diǎn)

獲取某個(gè)節(jié)點(diǎn)的父節(jié)點(diǎn)可以調(diào)用parent屬性,例如獲取實(shí)例中title節(jié)點(diǎn)的父節(jié)點(diǎn):

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.title.parent)

【運(yùn)行結(jié)果】

<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type"/>
<meta content="IE=Edge" http-equiv="X-UA-Compatible"/>
<meta content="always" name="referrer"/>
<link rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="stylesheet" type="text/css"/>
<title>百度一下,你就知道 </title>
</head>

同理,如果是想要獲取節(jié)點(diǎn)的祖先節(jié)點(diǎn),則可調(diào)用parents屬性。

(3)兄弟節(jié)點(diǎn)

調(diào)用next_sibling獲取節(jié)點(diǎn)的下一個(gè)兄弟元素;

調(diào)用previous_sibling獲取節(jié)點(diǎn)的上一個(gè)兄弟元素;

調(diào)用next_siblings取節(jié)點(diǎn)的下一個(gè)兄弟節(jié)點(diǎn);

調(diào)用previous_siblings獲取節(jié)點(diǎn)的上一個(gè)兄弟節(jié)點(diǎn);

4、方法選擇器

find_all()

查找所有符合條件的元素,其使用方法如下:

find_all(name,attrs,recursive,text,**kwargs)

(1)name

根據(jù)節(jié)點(diǎn)名來(lái)查詢?cè)兀绮樵儗?shí)例中a標(biāo)簽元素:

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(name = "a"))
for a in soup.find_all(name = "a"):
    print(a)

【運(yùn)行結(jié)果】

[<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trhao123">hao123 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trmap">地圖 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trvideo">視頻 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trtieba">貼吧 </a>, <a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>]
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trhao123">hao123 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trmap">地圖 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trvideo">視頻 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trtieba">貼吧 </a>
<a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>

(2)attrs

在查詢時(shí)我們還可以傳入標(biāo)簽的屬性,attrs參數(shù)的數(shù)據(jù)類型是字典。

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(name = "a",attrs = {"class":"bri"}))

【運(yùn)行結(jié)果】

[<a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>]

可以看到,在加上class=“bri”屬性時(shí),查詢結(jié)果就只剩一條a標(biāo)簽元素。

(3)text

text參數(shù)可以用來(lái)匹配節(jié)點(diǎn)的文本,傳入的可以是字符串,也可以是正則表達(dá)式對(duì)象。

import re
from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.find_all(name = "a",text = re.compile('新聞')))

【運(yùn)行結(jié)果】

[<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>]

只包含文本內(nèi)容為“新聞”的a標(biāo)簽。

find()

find()的使用與前者相似,唯一不同的是,find進(jìn)匹配搜索到的第一個(gè)元素,然后返回單個(gè)元素,find_all()則是匹配所有符合條件的元素,返回一個(gè)列表。

5、CSS選擇器

使用CSS選擇器時(shí),調(diào)用select()方法,傳入相應(yīng)的CSS選擇器;

例如使用CSS選擇器獲取實(shí)例中的a標(biāo)簽

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
print(soup.select('a'))
for a in soup.select('a'):
    print(a)

【運(yùn)行結(jié)果】

[<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trhao123">hao123 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trmap">地圖 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trvideo">視頻 </a>, <a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trtieba">貼吧 </a>, <a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>]
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trnews">新聞 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trhao123">hao123 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trmap">地圖 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trvideo">視頻 </a>
<a class="mnav" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_trtieba">貼吧 </a>
<a class="bri" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a>

獲取屬性

獲取上述a標(biāo)簽中的href屬性

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
for a in soup.select('a'):
    print(a['href'])

【運(yùn)行結(jié)果】

http://news.baidu.com
https://www.hao123.com
http://map.baidu.com
http://v.baidu.com
http://tieba.baidu.com
//www.baidu.com/more/

獲取文本

獲取上述a標(biāo)簽的文本內(nèi)容,使用get_text()方法,或者是string獲取文本內(nèi)容

from bs4 import BeautifulSoup

file = open("./test03.html",'rb')
html = file.read()
soup = BeautifulSoup(html,'lxml')
for a in soup.select('a'):
    print(a.get_text())
    print(a.string)

【運(yùn)行結(jié)果】

新聞
新聞
hao123
hao123
地圖
地圖
視頻
視頻
貼吧
貼吧
更多產(chǎn)品
更多產(chǎn)品

到此這篇關(guān)于python網(wǎng)絡(luò)爬蟲精解之Beautiful Soup的使用說(shuō)明的文章就介紹到這了,更多相關(guān)python Beautiful Soup 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程

    使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程

    這篇文章主要介紹了使用Python發(fā)送郵件附件以定時(shí)備份MySQL的教程,本文的示例基于CentOS,需要的朋友可以參考下
    2015-04-04
  • Python中requests做接口測(cè)試的方法

    Python中requests做接口測(cè)試的方法

    Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),編寫爬蟲和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,本文主要介紹了Python中requests做接口測(cè)試的方法,感興趣的可以了解一下
    2021-05-05
  • 淺析Python中生成器和迭代器的神奇力量

    淺析Python中生成器和迭代器的神奇力量

    在Python中,生成器和迭代器是實(shí)現(xiàn)惰性計(jì)算的兩種重要工具,它們可以幫助我們更有效地處理數(shù)據(jù)接下來(lái),小編就來(lái)為大家詳細(xì)介紹這兩種工具吧
    2023-07-07
  • python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法

    python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法

    今天小編就為大家分享一篇python實(shí)現(xiàn)n個(gè)數(shù)中選出m個(gè)數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • django之用戶、用戶組及權(quán)限設(shè)置方式

    django之用戶、用戶組及權(quán)限設(shè)置方式

    這篇文章主要介紹了django之用戶、用戶組及權(quán)限設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python繪圖操作之turtle庫(kù)烏龜繪圖全面整理

    Python繪圖操作之turtle庫(kù)烏龜繪圖全面整理

    Turtle庫(kù)是Python語(yǔ)言中一個(gè)很流行的繪制圖像的函數(shù)庫(kù),想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開(kāi)始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中移動(dòng),從而在它爬行的路徑上繪制了圖形
    2021-10-10
  • python灰色預(yù)測(cè)法的具體使用

    python灰色預(yù)測(cè)法的具體使用

    灰色系統(tǒng)理論認(rèn)為對(duì)既含有已知信息又含有未知或非確定信息的系統(tǒng)進(jìn)行預(yù)測(cè),本文就介紹了python灰色預(yù)測(cè)法的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-03-03
  • Pytorch1.5.1版本安裝的方法步驟

    Pytorch1.5.1版本安裝的方法步驟

    這篇文章主要介紹了Pytorch1.5.1版本安裝的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python之class類和方法的用法詳解

    python之class類和方法的用法詳解

    這篇文章主要介紹了python中class類和方法的用法詳解,如果有不太清楚面向?qū)ο蟮念惡头椒ǖ木幊趟枷氲男』锇榭梢越梃b參考本文
    2023-03-03
  • 在spyder IPython console中,運(yùn)行代碼加入?yún)?shù)的實(shí)例

    在spyder IPython console中,運(yùn)行代碼加入?yún)?shù)的實(shí)例

    這篇文章主要介紹了在spyder IPython console中,運(yùn)行代碼加入?yún)?shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04

最新評(píng)論

彭水| 北宁市| 平度市| 泽库县| 景洪市| 田林县| 宜都市| 绿春县| 上栗县| 阿拉善盟| 博客| 长治县| 泾川县| 轮台县| 云林县| 柘城县| 烟台市| 库车县| 琼结县| 舟山市| 建水县| 肥东县| 曲松县| 庄河市| 昌平区| 岚皋县| 蛟河市| 建始县| 无为县| 潼关县| 凌海市| 弥渡县| 伊通| 望奎县| 郧西县| 黑河市| 鹿邑县| 富源县| 静海县| 凤城市| 北海市|