創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
今天就跟大家聊聊有關(guān)如何安裝了BS4庫(kù),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Beautiful Soup 庫(kù)一般被稱為bs4庫(kù),支持Python3,是我們寫爬蟲非常好的第三方庫(kù)。因用起來(lái)十分的簡(jiǎn)便流暢。所以也被人叫做“美味湯”。目前bs4庫(kù)的最新版本是4.60。下文會(huì)介紹該庫(kù)的最基本的使用,具體詳細(xì)的細(xì)節(jié)還是要看:[官方文檔](Beautiful Soup Documentation)
bs4庫(kù)的安裝
Python的強(qiáng)大之處就在于他作為一個(gè)開源的語(yǔ)言,有著許多的開發(fā)者為之開發(fā)第三方庫(kù),這樣我們開發(fā)者在想要實(shí)現(xiàn)某一個(gè)功能的時(shí)候,只要專心實(shí)現(xiàn)特定的功能,其他細(xì)節(jié)與基礎(chǔ)的部分都可以交給庫(kù)來(lái)做。bs4庫(kù) 就是我們寫爬蟲強(qiáng)有力的幫手。
安裝的方式非常簡(jiǎn)單:我們用pip工具在命令行里進(jìn)行安裝
pip install beautifulsoup4
接著我們看一下是否成功安裝了bs4庫(kù)
pip list
這樣我們就成功安裝了 bs4 庫(kù)
bs4庫(kù)的簡(jiǎn)單使用
這里我們先簡(jiǎn)單的講解一下bs4庫(kù)的使用,
暫時(shí)不去考慮如何從web上抓取網(wǎng)頁(yè),
假設(shè)我們需要爬取的html是如下這么一段:
下面的一段HTML代碼將作為例子被多次用到.這是 愛麗絲夢(mèng)游仙境的 的一段內(nèi)容(以后內(nèi)容中簡(jiǎn)稱為 愛麗絲 的文檔): <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 http://example.com/elsie" class="sister" id="link1">Elsie, http://example.com/lacie" class="sister" id="link2">Lacie and http://example.com/tillie" class="sister" id="link3">Tillie; and they lived at the bottom of a well.</p> <p class="story">...</p> </html>
下面我們開始用bs4庫(kù)解析這一段html網(wǎng)頁(yè)代碼。
#導(dǎo)入bs4模塊 from bs4 import BeautifulSoup #做一個(gè)美味湯 soup = BeautifulSoup(html,'html.parser') #輸出結(jié)果 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" > # Elsie # </a> # , # <a class="sister" > # Lacie # </a> # and # <a class="sister" > # Tillie # </a> # ; and they lived at the bottom of a well. # </p> # <p class="story"> # ... # </p> # </body> # </html> '''
可以看到bs4庫(kù)將網(wǎng)頁(yè)文件變成了一個(gè)soup的類型,
事實(shí)上,bs4庫(kù) 是解析、遍歷、維護(hù)、“標(biāo)簽樹“的功能庫(kù)。
通俗一點(diǎn)說(shuō)就是: bs4庫(kù)把html源代碼重新進(jìn)行了格式化,
從而方便我們對(duì)其中的節(jié)點(diǎn)、標(biāo)簽、屬性等進(jìn)行操作。
下面是幾個(gè)簡(jiǎn)單的瀏覽結(jié)構(gòu)化數(shù)據(jù)的方式 :
請(qǐng)仔細(xì)觀察最前面的html文件
# 找到文檔的title soup.title # <title>The Dormouse's story</title> #title的name值 soup.title.name # u'title' #title中的字符串String soup.title.string # u'The Dormouse's story' #title的父親節(jié)點(diǎn)的name屬性 soup.title.parent.name # u'head' #文檔的第一個(gè)找到的段落 soup.p # <p class="title"><b>The Dormouse's story</b></p> #找到的p的class屬性值 soup.p['class'] # u'title' #找到a標(biāo)簽 soup.a # http://example.com/elsie" id="link1">Elsie #找到所有的a標(biāo)簽 soup.find_all('a') # [http://example.com/elsie" id="link1">Elsie, # http://example.com/lacie" id="link2">Lacie, # http://example.com/tillie" id="link3">Tillie] #找到id值等于3的a標(biāo)簽 soup.find(id="link3") # http://example.com/tillie" id="link3">Tillie
通過(guò)上面的例子 我們知道bs4庫(kù)是這樣理解一個(gè)html源文件的:
首先 把html源文件轉(zhuǎn)換為soup類型
接著 從中通過(guò)特定的方式抓取內(nèi)容
更高級(jí)點(diǎn)的用法?
從文檔中找到所有<a>標(biāo)簽的鏈接:
#發(fā)現(xiàn)了沒有,find_all方法返回的是一個(gè)可以迭代的列表for link in soup.find_all('a'): print(link.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie
從文檔中獲取所有文字內(nèi)容:
#我們可以通過(guò)get_text 方法 快速得到源文件中的所有text內(nèi)容。 print(soup.get_text()) # The Dormouse's story # # The Dormouse's story # # Once upon a time there were three little sisters; and their names were # Elsie, # Lacie and # Tillie; # and they lived at the bottom of a well. # # ...
看完上述內(nèi)容,你們對(duì)如何安裝了BS4庫(kù)有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝大家的支持。
本文題目:如何安裝幫手BS4庫(kù)-創(chuàng)新互聯(lián)
本文URL:http://jinyejixie.com/article36/dpdpsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站導(dǎo)航、動(dòng)態(tài)網(wǎng)站、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容