Python BS4庫的安裝與使用詳解
Beautiful Soup 庫一般被稱為bs4庫,支持Python3,是我們寫爬蟲非常好的第三方庫。因用起來十分的簡便流暢。所以也被人叫做“美味湯”。目前bs4庫的最新版本是4.60。下文會(huì)介紹該庫的最基本的使用,具體詳細(xì)的細(xì)節(jié)還是要看:[官方文檔](Beautiful Soup Documentation)
bs4庫的安裝
Python的強(qiáng)大之處就在于他作為一個(gè)開源的語言,有著許多的開發(fā)者為之開發(fā)第三方庫,這樣我們開發(fā)者在想要實(shí)現(xiàn)某一個(gè)功能的時(shí)候,只要專心實(shí)現(xiàn)特定的功能,其他細(xì)節(jié)與基礎(chǔ)的部分都可以交給庫來做。bs4庫 就是我們寫爬蟲強(qiáng)有力的幫手。
安裝的方式非常簡單:我們用pip工具在命令行里進(jìn)行安裝
$ pip install beautifulsoup4
接著我們看一下是否成功安裝了bs4庫
$ pip list
這樣我們就成功安裝了 bs4 庫
bs4庫的簡單使用
這里我們先簡單的講解一下bs4庫的使用,
暫時(shí)不去考慮如何從web上抓取網(wǎng)頁,
假設(shè)我們需要爬取的html是如下這么一段:
下面的一段HTML代碼將作為例子被多次用到.這是 愛麗絲夢(mèng)游仙境的 的一段內(nèi)容(以后內(nèi)容中簡稱為 愛麗絲 的文檔):
<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庫解析這一段html網(wǎng)頁代碼。
#導(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" rel="external nofollow" id="link1"> # Elsie # </a> # , # <a class="sister" rel="external nofollow" id="link2"> # Lacie # </a> # and # <a class="sister" rel="external nofollow" id="link2"> # Tillie # </a> # ; and they lived at the bottom of a well. # </p> # <p class="story"> # ... # </p> # </body> # </html> '''
可以看到bs4庫將網(wǎng)頁文件變成了一個(gè)soup的類型,
事實(shí)上,bs4庫 是解析、遍歷、維護(hù)、“標(biāo)簽樹“的功能庫。
通俗一點(diǎn)說就是: bs4庫把html源代碼重新進(jìn)行了格式化,
從而方便我們對(duì)其中的節(jié)點(diǎn)、標(biāo)簽、屬性等進(jìn)行操作。
下面是幾個(gè)簡單的瀏覽結(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
通過上面的例子 我們知道bs4庫是這樣理解一個(gè)html源文件的:
首先 把html源文件轉(zhuǎn)換為soup類型
接著 從中通過特定的方式抓取內(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)容:
#我們可以通過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. # # ...
bs4庫的入門使用我們就先進(jìn)行到這。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
圖文詳解Python中模塊或py文件導(dǎo)入(超詳細(xì)!)
導(dǎo)入文件目的就是為了執(zhí)行文件,下面這篇文章主要給大家介紹了關(guān)于Python中模塊或py文件導(dǎo)入的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Python下singleton模式的實(shí)現(xiàn)方法
這篇文章主要介紹了Python下singleton模式的實(shí)現(xiàn)方法,有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07Python實(shí)現(xiàn)字典的遍歷與排序功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)字典的遍歷與排序功能,結(jié)合實(shí)例形式分析了Python字典的遍歷與排序相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2017-12-12Python如何獲取實(shí)時(shí)股票信息的方法示例
本文主要介紹了Python如何獲取實(shí)時(shí)股票信息的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06實(shí)例探究Python以并發(fā)方式編寫高性能端口掃描器的方法
端口掃描器就是向一批端口上發(fā)送請(qǐng)求來檢測(cè)端口是否打開的程序,這里我們以實(shí)例探究Python以并發(fā)方式編寫高性能端口掃描器的方法2016-06-06