Python之解BS4庫如何安裝與使用?正確方法教你


 

Beautiful Soup 庫一般被稱為bs4庫,支持Python3,是我們寫爬蟲非常好的第三方庫。因用起來十分的簡便流暢。所以也被人叫做“美味湯”。目前bs4庫的最新版本是4.60。下文會介紹該庫的最基本的使用,具體詳細的細節還是要看:[官方文檔](Beautiful Soup Documentation)

bs4庫的安裝

Python的強大之處就在於他作為一個開源的語言,有着許多的開發者為之開發第三方庫,這樣我們開發者在想要實現某一個功能的時候,只要專心實現特定的功能,其他細節與基礎的部分都可以交給庫來做。bs4庫 就是我們寫爬蟲強有力的幫手。另外要注意:光理論是不夠的。這里順便免費送大家一套2020最新python入門到高級項目實戰視頻教程,可以去小編的Python交流.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,還可以跟老司機交流討教!

安裝的方式非常簡單:我們用pip工具在命令行里進行安裝

pip install beautifulsoup4

接着我們看一下是否成功安裝了bs4庫

pip list

這樣我們就成功安裝了 bs4 庫

a09ef4f32c6ca5ee33a7b862b5f782b.png

bs4庫的簡單使用

這里我們先簡單的講解一下bs4庫的使用,

暫時不去考慮如何從web上抓取網頁,

假設我們需要爬取的html是如下這么一段:

下面的一段HTML代碼將作為例子被多次用到.這是 愛麗絲夢游仙境的 的一段內容(以后內容中簡稱為 愛麗絲 的文檔):

<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網頁代碼。

#導入bs4模塊
from bs4 import BeautifulSoup
#做一個美味湯
soup = BeautifulSoup(html,'html.parser')
#輸出結果
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="link2">
#     Tillie
#    </a>
#    ; and they lived at the bottom of a well.
#   </p>
#   <p class="story">
#    ...
#   </p>
#  </body>
# </html>
'''

可以看到bs4庫將網頁文件變成了一個soup的類型,

事實上,bs4庫 是解析、遍歷、維護、“標簽樹“的功能庫。

通俗一點說就是: bs4庫把html源代碼重新進行了格式化,

從而方便我們對其中的節點、標簽、屬性等進行操作。

下面是幾個簡單的瀏覽結構化數據的方式 :

請仔細觀察最前面的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的父親節點的name屬性
soup.title.parent.name
# u'head'
    
#文檔的第一個找到的段落
soup.p
# <p class="title"><b>The Dormouse's story</b></p>
    
#找到的p的class屬性值
soup.p['class']
# u'title'
    
#找到a標簽
soup.a
# http://example.com/elsie" id="link1">Elsie
    
#找到所有的a標簽
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標簽
soup.find(id="link3")
# http://example.com/tillie" id="link3">Tillie

通過上面的例子 我們知道bs4庫是這樣理解一個html源文件的:

首先 把html源文件轉換為soup類型

接着 從中通過特定的方式抓取內容

更高級點的用法?

從文檔中找到所有<a>標簽的鏈接:

#發現了沒有,find_all方法返回的是一個可以迭代的列表for link in soup.find_all('a'):
    print(link.get('href'))    # http://example.com/elsie
    # http://example.com/lacie
    # http://example.com/tillie

從文檔中獲取所有文字內容:

#我們可以通過get_text 方法 快速得到源文件中的所有text內容。
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.

最后注意:光理論是不夠的。這里順便免費送大家一套2020最新python入門到高級項目實戰視頻教程,可以去小編的Python交流.裙 :七衣衣九七七巴而五(數字的諧音)轉換下可以找到了,還可以跟老司機交流討教!
本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM