欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

bs4庫解析器怎么使用-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

公司主營業(yè)務(wù):網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè)、移動網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊有機(jī)會用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)公司推出河津免費做網(wǎng)站回饋大家。

這篇文章將為大家詳細(xì)講解有關(guān)bs4庫解析器怎么使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

bs4庫之所以能快速的定位我們想要的元素,是因為它能夠用一種方式將html文件解析了一遍 ,不同的解析器有不同的效果。

bs4解析器的選擇

網(wǎng)絡(luò)爬蟲的最終目的就是過濾選取網(wǎng)絡(luò)信息,最重要的部分可以說是解析器。解析器的優(yōu)劣決定了爬蟲的速度和效率。bs4庫除了支持我們上文用過的‘html.parser’解析器外,還支持很多第三方的解析器,下面我們來對他們進(jìn)行對比分析。

bs4庫官方推薦我們使用的是lxml解析器,原因是它具有更高的效率,所以我們也將采用lxml解析器。

lxml解析器的安裝:

依舊采用pip安裝工具來安裝:

pip install lxml

使用lxml解析器來解釋網(wǎng)頁

我們以愛麗絲文檔 為例子

 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 href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """

嘗試一下

import bs4
    
    
#首先我們先將html文件已lxml的方式做成一鍋湯
soup = bs4.BeautifulSoup(open('Beautiful Soup 爬蟲/demo.html'),'lxml')
    
#我們把結(jié)果輸出一下,是一個很清晰的樹形結(jié)構(gòu)。
#print(soup.prettify())
    
'''
OUT:
    
<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" href="http://example.com/elsie" id="link1">
    Elsie
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>
'''

如何具體的使用?

bs4 庫首先將傳入的字符串或文件句柄轉(zhuǎn)換為 Unicode的類型,這樣,我們在抓取中文信息的時候,就不會有很麻煩的編碼問題了。當(dāng)然,有一些生僻的編碼 如:‘big5’,就需要我們手動設(shè)置編碼:

soup = BeautifulSoup(markup, from_encoding="編碼方式")

對象的種類:

bs4 庫將復(fù)雜的html文檔轉(zhuǎn)化為一個復(fù)雜的樹形結(jié)構(gòu),每個節(jié)點都是Python對象 ,所有對象可以分為以下四個類型:Tag , NavigableString , BeautifulSoup , Comment

我們來逐一解釋:

Tag: 和html中的Tag基本沒有區(qū)別,可以簡單上手使用

NavigableString: 被包裹在tag內(nèi)的字符串

BeautifulSoup: 表示一個文檔的全部內(nèi)容,大部分的時候可以吧他看做一個tag對象,支持遍歷文檔樹和搜索文檔樹方法。

Comment:這是一個特殊的NavigableSting對象,在出現(xiàn)在html文檔中時,會以特殊的格式輸出,比如注釋類型。

搜索文檔樹的最簡單的方法就是搜索你想獲取tag的的name:

soup.head
# <head><title>The Dormouse's story</title></head>

soup.title
# <title>The Dormouse's story</title>

如果你還想更深入的獲得更小的tag:例如我們想找到body下的被b標(biāo)簽包裹的部分

soup.body.b
# <b>The Dormouse's story</b>

但是這個方法只能找到按順序第一個出現(xiàn)的tag

獲取所有的標(biāo)簽?zāi)兀?/p>

這個時候需要find_all()方法,他返回一個列表類型

tag=soup.find_all('a')
# [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>,
#  <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>,
#  <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
#假設(shè)我們要找到a標(biāo)簽中的第二個元素:
need = tag[1]

tag的.contents屬性可以將tag的子節(jié)點以列表的方式輸出:

head_tag = soup.head
head_tag
# <head><title>The Dormouse's story</title></head>

head_tag.contents
[<title>The Dormouse's story</title>]
title_tag = head_tag.contents[0]
print(title_tag)
# <title>The Dormouse's story</title>
title_tag.contents
# [u'The Dormouse's story']

另外通過tag的 .children生成器,可以對tag的子節(jié)點進(jìn)行循環(huán):

for child in title_tag.children:
    print(child)
    # The Dormouse's story

這種方式只能遍歷出子節(jié)點。如何遍歷出子孫節(jié)點呢?

子孫節(jié)點:比如 head.contents 的子節(jié)點是<title>The Dormouse's story</title>,這里 title本身也有子節(jié)點:‘The Dormouse‘s story’ 。這里的‘The Dormouse‘s story’也叫作head的子孫節(jié)點

for child in head_tag.descendants:
    print(child)
    # <title>The Dormouse's story</title>
    # The Dormouse's story

如何找到tag下的所有的文本內(nèi)容呢?

1、如果該tag只有一個子節(jié)點(NavigableString類型):直接使用tag.string就能找到。

2、如果tag有很多個子、孫節(jié)點,并且每個節(jié)點里都string:

我們可以用迭代的方式將其全部找出:

for string in soup.strings:
    print(repr(string))
    # u"The Dormouse's story"
    # u'\n\n'
    # u"The Dormouse's story"
    # u'\n\n'
    # u'Once upon a time there were three little sisters; and their names were\n'
    # u'Elsie'
    # u',\n'
    # u'Lacie'
    # u' and\n'
    # u'Tillie'
    # u';\nand they lived at the bottom of a well.'
    # u'\n\n'
    # u'...'
    # u'\n'

關(guān)于bs4庫解析器怎么使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文名稱:bs4庫解析器怎么使用-創(chuàng)新互聯(lián)
本文來源:http://www.chinadenli.net/article26/digjjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)標(biāo)簽優(yōu)化電子商務(wù)外貿(mào)網(wǎng)站建設(shè)全網(wǎng)營銷推廣軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司