国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python BS4庫的安裝與使用詳解

2020-02-15 22:42:51
字體:
來源:轉載
供稿:網友

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

bs4庫的安裝

Python的強大之處就在于他作為一個開源的語言,有著許多的開發者為之開發第三方庫,這樣我們開發者在想要實現某一個功能的時候,只要專心實現特定的功能,其他細節與基礎的部分都可以交給庫來做。bs4庫 就是我們寫爬蟲強有力的幫手。

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

$ pip install beautifulsoup4

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

$ pip list

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

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 werehttp://example.com/elsie" class="sister" id="link1">Elsie,http://example.com/lacie" class="sister" id="link2">Lacie andhttp://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"  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>'''            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 克拉玛依市| 迭部县| 织金县| 康乐县| 塔河县| 汉阴县| 开鲁县| 武隆县| 常熟市| 青海省| 讷河市| 毕节市| 资阳市| 柳江县| 马龙县| 城固县| 黎城县| 巴彦淖尔市| 建昌县| 阿拉善左旗| 那坡县| 社旗县| 阜城县| 扎囊县| 湖州市| 大洼县| 洛扎县| 印江| 绍兴县| 共和县| 佳木斯市| 景洪市| 彭泽县| 含山县| 内黄县| 乌苏市| 宝兴县| 玛曲县| 宜兴市| 南木林县| 拜泉县|