BeautifulSoup是Python爬蟲應用解析Html的利器,是Python三方模組bs4中提供的進行HTML解析的類,可以認為是一個HTML解析工具箱,對HTML報文中的標籤具有比較好的容錯識別功能。lxml是一款html文字解析器,BeautifulSoup構建物件時需要指定HTML解析器,推薦使用lxml。
BeautifulSoup和lxml安裝命令:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple bs4
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple lxml
載入BeautifulSoup:
from bs4 import BeautifulSoup
BeatifulSoap解析HTML報文的常用功能:
from bs4 import BeautifulSoup
import urllib.request
def getURLinf(url):
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}
req = urllib.request.Request(url=url,headers=header)
resp = urllib.request.urlopen(req,timeout=5)
html = resp.read().decode()
soup = BeautifulSoup(html,'lxml')
return (soup,req,resp)
soup,req ,resp = getURLinf(r'https://blog.csdn.net/LaoYuanPython/article/details/111303395')
print(soup.p)
print(soup.link)
print(soup.title)
print(soup.link.attrs)
print(soup.link['rel'])
print(soup.body.contents)
具體的大家可以參考老猿部落格的免費專欄《爬蟲:https://blog.csdn.net/laoyuanpython/category_9103810.html》或付費專欄《Python爬蟲入門:https://blog.csdn.net/laoyuanpython/category_10762553.html》的相關介紹。
在HTML解析時,如果通過簡單的tag、或單個tag屬性(如id、class)或文字一次搜尋或select定位是最簡單的,而有些情況需要使用組合方法才能處理。
經常有些要定位的標籤有很多,按單個屬性查詢也有很多,得使用多個屬性查詢。如:
<div id="article_content" class="article_content clearfix">
......
</div>
<div id="article_content" class="article_view">
......
</div>
<div id="article_view" class="article_view">
......
</div>
上面的html文字中有多個id為article_content的div標籤,如果使用:
>>> text="""```html
<div id="article_content" class="article_content clearfix">
......
</div>
<div id="article_content" class="article_view">
......
</div>
<div id="article_view" class="article_view">
......
</div>"""
>>> s = BeautifulSoup(text,'lxml')
>>> s.select('div#article_content')
[<div class="article_content clearfix" id="article_content">......</div>,
<div class="article_view" id="article_content">......</div>]
>>>
就會返回兩條記錄。這時候就可以使用多標籤屬性定位的如下2種語句:
>>>s.select('div#article_content[class="article_content clearfix"]')
[<div class="article_content clearfix" id="article_content">......</div>]
>>>s.select('div[id="article_content"][class="article_content clearfix"]')
[<div class="article_content clearfix" id="article_content">......</div>]
>>>s.find_all("div",id="article_content",class_='article_content clearfix')
[<div class="article_content clearfix" id="article_content">......</div>]
>>>s.find_all("div","#article_content",class_='article_content clearfix')
[<div class="article_content clearfix" id="article_content">......</div>]
以上四種方式是等價的,因為id可以用#來標記,class在查詢時需要和Python關鍵字class區分,因此有上述不同方法,注意select的每個屬性必須用中括號括起來,不同屬性的中括號之間不能有空格。
tag標籤關係包括父子、兄弟、祖先等關係,有時要查詢或定位的內容本身不是很好定位,但結合其他標籤關係(主要是父子、祖先關係)則可以唯一確認。
案例:
這是CSDN的博文中關於博主個人資訊的部分報文:
<div class="data-info d-flex item-tiling">
<dl class="text-center" title="1055">
<a href="https://blog.csdn.net/LaoYuanPython" data-report-click='{"mod":"1598321000_001","spm":"1001.2101.3001.4310"}' data-report-query="t=1">
<dt><span class="count">1055</span></dt>
<dd class="font">原創</dd>
</a>
</dl>
<dl class="text-center" data-report-click='{"mod":"1598321000_002","spm":"1001.2101.3001.4311"}' title="22">
<a href="https://blog.csdn.net/rank/writing_rank" target="_blank">
<dt><span class="count">22</span></dt>
<dd class="font">周排名</dd>
</a>
</dl>
</div>
以上報文中,如果要取博主的原創文章數和周排名,原創文章數和博主周排名的tag標籤完全相同,二者都在span標籤內,標籤的屬性及值都相同,只是span標籤的父標籤dt標籤的兄弟標籤dd標籤的string的中文內容才能區分。對於這種情況,首先要通過祖先標籤<div class="data-info d-flex item-tiling">
定位到祖先標籤,再在祖先標籤內通過中文字串定位到要存取屬性的兄弟標籤的子標籤,然後通過該子標籤找到其父標籤的父標籤,再通過該父標籤的dt子標籤的span子標籤存取具體取值。
範例程式碼如下:
>>> text="""
<div class="data-info d-flex item-tiling">
<dl class="text-center" title="1055">
<a href="https://blog.csdn.net/LaoYuanPython" data-report-click='{"mod":"1598321000_001","spm":"1001.2101.3001.4310"}' data-report-query="t=1">
<dt><span class="count">1055</span></dt>
<dd class="font">原創</dd>
</a>
</dl>
<dl class="text-center" data-report-click='{"mod":"1598321000_002","spm":"1001.2101.3001.4311"}' title="22">
<a href="https://blog.csdn.net/rank/writing_rank" target="_blank">
<dt><span class="count">22</span></dt>
<dd class="font">周排名</dd>
</a>
</dl>
</div>"""
>>> s = BeautifulSoup(text,'lxml')
>>> subSoup = s.select('[class="data-info d-flex item-tiling"] [class="font"]')
>>> for item in subSoup:
parent = item.parent
if item.string=='原創':
orignalNum = int(parent.select('.count')[0].string)
elif item.string=='周排名':
weekRank = int(parent.select('.count')[0].string)
>>> print(orignalNum,weekRank)
1055 22
>>>
在解析HTML報文時,絕大多數情況是需要分析有用的標籤資訊,但作為技術文章,大部分的博文中都有程式碼,這些程式碼可能會對分析進行干擾。如本文中的程式碼含有一些分析的HTML報文,如果獲取本文的完整HTML內容,這些報文在非程式碼部分也會出現,此時要排除程式碼的影響,可以將程式碼先從分析內容中去除再來分析。
目前大多數技術平臺的博文編輯器都支援對程式碼的標識,象markdown等編輯器程式碼的標籤為code標檢,如果有其他編輯器用不同標籤的,只有確認了標籤名,都可以按下面介紹的類似方式來處理。
處理步驟如下:
關於這部分內容的案例可以參考《https://blog.csdn.net/LaoYuanPython/article/details/114729045 n行Python程式碼系列:四行程式分離HTML報文中的程式程式碼》的詳細介紹。
本文介紹了使用BeatifulSoap解析HTML報文的三個使用技巧,包括通過多屬性組合查詢或定位標籤、通過結合多個標籤關係來定位標籤以及去除html報文中的程式碼標籤來避免程式碼對解析的影響。
如果閱讀本文於您有所獲,敬請點贊、評論、收藏,謝謝大家的支援!
前兩個專欄都適合有一定Python基礎但無相關知識的小白讀者學習,第三個專欄請大家結合《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python圖形影象處理 》的學習使用。
對於缺乏Python基礎的同仁,可以通過老猿的免費專欄《https://blog.csdn.net/laoyuanpython/category_9831699.html 專欄:Python基礎教學目錄)從零開始學習Python。
如果有興趣也願意支援老猿的讀者,歡迎購買付費專欄。