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

Python 爬蟲之Beautiful Soup模塊使用指南

 更新時間:2018年07月05日 14:43:46   作者:hoxis  
這篇文章主要介紹了Python 爬蟲之Beautiful Soup模塊使用指南,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

爬取網(wǎng)頁的流程一般如下:

  1. 選著要爬的網(wǎng)址(url)
  2. 使用 python 登錄上這個網(wǎng)址(urlopen、requests 等)
  3. 讀取網(wǎng)頁信息(read() 出來)
  4. 將讀取的信息放入 BeautifulSoup
  5. 使用 BeautifulSoup 選取 tag 信息等

可以看到,頁面的獲取其實不難,難的是數(shù)據(jù)的篩選,即如何獲取到自己想要的數(shù)據(jù)。本文就帶大家學(xué)習(xí)下 BeautifulSoup 的使用。

BeautifulSoup 官網(wǎng)介紹如下:

Beautiful Soup 是一個可以從 HTML 或 XML 文件中提取數(shù)據(jù)的 Python 庫,它能夠通過你喜歡的轉(zhuǎn)換器實現(xiàn)慣用的文檔導(dǎo)航、查找、修改文檔的方式,能夠幫你節(jié)省數(shù)小時甚至數(shù)天的工作時間。

1 安裝

可以利用 pip 直接安裝:

$ pip install beautifulsoup4

BeautifulSoup 不僅支持 HTML 解析器,還支持一些第三方的解析器,如 lxml,XML,html5lib 但是需要安裝相應(yīng)的庫。如果我們不安裝,則 Python 會使用 Python 默認(rèn)的解析器,其中 lxml 解析器更加強(qiáng)大,速度更快,推薦安裝。

$ pip install html5lib
$ pip install lxml

2 BeautifulSoup 的簡單使用

首先我們先新建一個字符串,后面就以它來演示 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  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link1">Elsie</a>,
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and
<a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

使用 BeautifulSoup 解析這段代碼,能夠得到一個 BeautifulSoup 的對象,并能按照標(biāo)準(zhǔn)的縮進(jìn)格式的結(jié)構(gòu)輸出:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html_doc, "lxml")
>>> print(soup.prettify())

篇幅有限,輸出結(jié)果這里不再展示。

另外,這里展示下幾個簡單的瀏覽結(jié)構(gòu)化數(shù)據(jù)的方法:

>>> soup.title
<title>The Dormouse's story</title>
>>> soup.title.name
'title'
>>> soup.title.string
"The Dormouse's story"
>>> soup.p['class']
['title']
>>> soup.a
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>
>>> soup.find_all('a')
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]
>>> soup.find(id='link1')
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>

3 對象的種類

Beautiful Soup 將復(fù)雜 HTML 文檔轉(zhuǎn)換成一個復(fù)雜的樹形結(jié)構(gòu),每個節(jié)點(diǎn)都是 Python 對象,所有對象可以歸納為 4 種: Tag、NavigableString、BeautifulSoup、Comment 。

3.1 Tag

Tag通俗點(diǎn)講就是 HTML 中的一個個標(biāo)簽,像上面的 div,p,例如:

<title>The Dormouse's story</title>
  
<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>

可以利用 soup 加標(biāo)簽名輕松地獲取這些標(biāo)簽的內(nèi)容。

>>> print(soup.p)
<p class="title"><b>The Dormouse's story</b></p>
>>> print(soup.title)
<title>The Dormouse's story</title>

不過有一點(diǎn)是,它查找的是在所有內(nèi)容中的第一個符合要求的標(biāo)簽,如果要查詢所有的標(biāo)簽,我們在后面進(jìn)行介紹。

每個 Tag 有兩個重要的屬性 name 和 attrs,name 指標(biāo)簽的名字或者 tag 本身的 name,attrs 通常指一個標(biāo)簽的 class。

>>> print(soup.p.name)
p
>>> print(soup.p.attrs)
{'class': ['title']}

3.2 NavigableString

NavigableString:獲取標(biāo)簽內(nèi)部的文字,如,soup.p.string。

>>> print(soup.p.string)
The Dormouse's story

3.3 BeautifulSoup

BeautifulSoup:表示一個文檔的全部內(nèi)容。大部分時候,可以把它當(dāng)作 Tag 對象,是一個特殊的 Tag。

3.4 Comment

Comment:Comment 對象是一個特殊類型的 NavigableString 對象,其輸出的內(nèi)容不包括注釋符號,但是如果不好好處理它,可能會對我們的文本處理造成意想不到的麻煩。

>>> markup = "<b><!--Hey, buddy. Want to buy a used parser?--></b>"
>>> soup = BeautifulSoup(markup)
>>> comment = soup.b.string
>>> print(comment)
Hey, buddy. Want to buy a used parser?
>>> type(comment)
<class 'bs4.element.Comment'>

b 標(biāo)簽里的內(nèi)容實際上是注釋,但是如果我們利用 .string 來輸出它的內(nèi)容,我們發(fā)現(xiàn)它已經(jīng)把注釋符號去掉了,所以這可能會給我們帶來不必要的麻煩。

這時候我們可以先判斷了它的類型,是否為 bs4.element.Comment 類型,然后再進(jìn)行其他操作,如打印輸出等。

4 搜索文檔樹

BeautifulSoup 主要用來遍歷子節(jié)點(diǎn)及子節(jié)點(diǎn)的屬性,并提供了很多方法,比如獲取 子節(jié)點(diǎn)、父節(jié)點(diǎn)、兄弟節(jié)點(diǎn)等,但通過實踐來看,這些方法用到的并不多。我們主要用到的是從文檔樹中搜索出我們的目標(biāo)。

通過點(diǎn)取屬性的方式只能獲得當(dāng)前文檔中的第一個 tag,例如,soup.li。如果想要得到所有的<li> 標(biāo)簽,就需要用到 find_all(),find_all() 方法搜索當(dāng)前 tag 的所有 tag 子節(jié)點(diǎn),并判斷是否符合過濾器的條件 find_all() 所接受的參數(shù)如下:

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

4.1 按 name 搜索

可以查找所有名字為 name 的 tag,字符串對象會被自動忽略掉。

>>> soup.find_all('b')
[<b>The Dormouse's story</b>]
>>> soup.find_all('a')
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

4.2 按 id 搜索

如果文檔樹中包含一個名字為 id 的參數(shù),其實在搜索時會把該參數(shù)當(dāng)作指定名字 tag 的屬性來搜索:

>>> soup.find_all(id='link1')
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>]

4.3 按 attr 搜索

有些 tag 屬性在搜索不能使用,比如 HTML5 中的 data-* 屬性,但是可以通過 find_all() 方法的 attrs 參數(shù)定義一個字典參數(shù)來搜索包含特殊屬性的 tag。

其實 id 也是一個 attr:

>>> soup.find_all(attrs={'id':'link1'})
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>]

4.4 按 CSS 搜索

按照 CSS 類名搜索 tag 的功能非常實用,但標(biāo)識 CSS 類名的關(guān)鍵字 class 在 Python 中是保留字,使用 class 做參數(shù)會導(dǎo)致語法錯誤。因此從 Beautiful Soup 的 4.1.1 版本開始,可以通過 class_ 參數(shù)搜索有指定 CSS 類名的 tag:

>>> soup.find_all(class_='sister')
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link2">Lacie</a>, <a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link3">Tillie</a>]

4.5 string 參數(shù)

通過 string 參數(shù)可以搜搜文檔中的字符串內(nèi)容。與 name 參數(shù)的可選值一樣,string 參數(shù)接受字符串、正則表達(dá)式、列表、True。

>>> soup.find_all('a', string='Elsie')
[<a class="sister"  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="link1">Elsie</a>]

4.6 recursive 參數(shù)

調(diào)用 tag 的 find_all() 方法時,Beautiful Soup 會檢索當(dāng)前 tag 的所有子孫節(jié)點(diǎn),如果只想搜索 tag 的直接子節(jié)點(diǎn),可以使用參數(shù) recursive=False。

4.6 find() 方法

它與 find_all() 方法唯一的區(qū)別是 find_all() 方法的返回結(jié)果是值包含一個元素的列表,而 find() 方法只返回第一個匹配的結(jié)果。

4.7 get_text() 方法

如果只想得到 tag 中包含的文本內(nèi)容,那么可以用 get_text() 方法,這個方法獲取到 tag 中包含的所有文本內(nèi)容。

>>> soup.find_all('a', string='Elsie')[0].get_text()
'Elsie'
>>> soup.find_all('a', string='Elsie')[0].string
'Elsie'

至此,Beautiful Soup 的常用使用方法已講完,若果想了解更多內(nèi)容,建議看下官方文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/。

總結(jié)

本篇主要帶大家了解了 Beautiful Soup,結(jié)合一些小例子,相信大家對 Beautiful Soup 已不再陌生,下回會帶大家結(jié)合 Beautiful Soup 進(jìn)行爬蟲的實戰(zhàn),歡迎繼續(xù)關(guān)注!

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python 函數(shù)中的參數(shù)類型

    python 函數(shù)中的參數(shù)類型

    thon中函數(shù)的參數(shù)類型比較豐富,比如我們經(jīng)常見到*args和**kwargs作為參數(shù)。今天給大家介紹python 函數(shù)中的參數(shù)類型,需要的朋友可以參考下
    2020-02-02
  • Python實現(xiàn)內(nèi)網(wǎng)穿透和端口轉(zhuǎn)發(fā)代理詳解

    Python實現(xiàn)內(nèi)網(wǎng)穿透和端口轉(zhuǎn)發(fā)代理詳解

    這篇文章主要為大家介紹了Python實現(xiàn)內(nèi)網(wǎng)穿透和端口轉(zhuǎn)發(fā)代理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 用Python寫個新年賀卡生成器

    用Python寫個新年賀卡生成器

    大家好,本篇文章主要講的是用Python寫個新年賀卡生成器,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Python使用Flask框架同時上傳多個文件的方法

    Python使用Flask框架同時上傳多個文件的方法

    這篇文章主要介紹了Python使用Flask框架同時上傳多個文件的方法,實例分析了Python中Flask框架操作文件實現(xiàn)上傳的技巧,需要的朋友可以參考下
    2015-03-03
  • Python調(diào)用百度api實現(xiàn)語音識別詳解

    Python調(diào)用百度api實現(xiàn)語音識別詳解

    這篇文章主要介紹了Python通過調(diào)用百度api實現(xiàn)語音識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2021-12-12
  • python文件編寫好后如何實踐

    python文件編寫好后如何實踐

    在本篇文章里小編給大家分享了關(guān)于python文件編寫好后如何實踐的相關(guān)內(nèi)容,需要的朋友們可以參考下。
    2020-07-07
  • 詳解LyScript 內(nèi)存掃描與查殼實現(xiàn)

    詳解LyScript 內(nèi)存掃描與查殼實現(xiàn)

    這篇文章主要為大家介紹了詳解LyScript 內(nèi)存掃描與查殼實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • python+Django實現(xiàn)防止SQL注入的辦法

    python+Django實現(xiàn)防止SQL注入的辦法

    這篇文章主要介紹了python+Django實現(xiàn)防止SQL注入的辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python中dropna()函數(shù)的作用舉例說明

    python中dropna()函數(shù)的作用舉例說明

    這篇文章主要給大家介紹了關(guān)于python中dropna()函數(shù)的相關(guān)資料,dropna()是pandas庫中的一個函數(shù),用于刪除DataFrame中的缺失值,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • python魔法方法-自定義序列詳解

    python魔法方法-自定義序列詳解

    下面小編就為大家?guī)硪黄猵ython魔法方法-自定義序列詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07

最新評論

溧阳市| 堆龙德庆县| 安国市| 锡林浩特市| 乐平市| 鄂伦春自治旗| 郯城县| 游戏| 西和县| 屯门区| 阜阳市| 鞍山市| 中山市| 灵寿县| 南丰县| 山阳县| 鹤岗市| 株洲县| 沁水县| 宁都县| 罗定市| 宕昌县| 鸡泽县| 江源县| 临沂市| 康定县| 班玛县| 荔浦县| 金川县| 普格县| 扶风县| 浪卡子县| 永济市| 乌兰浩特市| 临邑县| 焦作市| 平舆县| 湛江市| 田东县| 罗山县| 会昌县|