熱線電話:13121318867

登錄
首頁大數據時代如何快速入門和使用beautifulsoup?
如何快速入門和使用beautifulsoup?
2020-07-15
收藏

BeautifulSoup是一款靈活又便捷的HTML/XML的解析器,通常被用來解析和提取 HTML/XML 數據。BeautifulSoup處理速度快,效率高,而且支持多種解析器,不用編寫正則表達式也能快速地實現網頁信息的提取。

1、BeautifulSoup與其他抓取工具的對比:

2、解析庫

3、安裝

(1)pip3 install beautifulsoup

(2)導入模塊:from bs4 import BeautifulSoup

(3) 創建BeautifulSoup對象

參數一:解析的文本內容

參數二:使用的解析器,一般為lxml(必須添加,否則會發出警告)

(4)格式化輸出 soup 對象的內容

4、基本使用

html = """

<html><head><title>The Dormouse's story</title></head>

<body>

<p class="title" name="dromouse"><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" id="link1"><!-- Elsie --></a>,

<a  class="sister" id="link2">Lacie</a> and

<a  class="sister" id="link3">Tillie</a>;

and they lived at the bottom of a well.</p>

<p class="story">...</p>

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html, 'lxml')#傳入解析器:lxml

print(soup.prettify())#格式化代碼,自動補全

print(soup.title.string)#得到title標簽里的內容

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢