通過HTMLPaser和urlib模塊對網頁進行抓取并分析
實現步驟:
1、自定義MYHTMLParser類
2、實例化類并訪問天氣預報官網
3、抓取關鍵數據
4、對數據進行切片處理并輸出字典
from html.parser import HTMLPaser? ?#處理頁面模塊
from urllib import request? #訪問網站模塊
L = []? #定義列表接受抓取的信息
class MYHTMLPaser(HTMLParser):? #自定義MYHTMLParser類重寫方法
? ? ? ? ? ? ? ? ? ? ? ? ? ? #定義三個函數
? ? ? ? ? ? ? ? ? ? ? ? ? ?#handle_starttag:處理開始標簽? attrs:標簽屬性 tag:標簽
? ? ? ? ? ? ? ? ? ? ? ? ? a_t = False #定義a_t變量為提出標簽內容時的判斷做準備
? ? ? ? ? ? ? ? ? ? ? ? ? ?def handle_starttag(self,tag,attrs):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if tag == "h1" and len(attrs) == 0:? #找出不帶屬性的h1標簽
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t = True
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?else? tag == "p" and len(attrs) > 1: #找出屬性大于1的p標簽
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t = True
? ? ? ? ? ? ? ? ? ? ? ? ? ? #handle_data:處理標簽內容 data:標簽內容
? ? ? ? ? ? ? ? ? ? ? ? ? ?def handle_data(self,data):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if self.a_t ==True:? #取出h1標簽p標簽的內容并添加到L列表中
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?L.append(data)
? ? ? ? ? ? ? ? ? ? ? ? ? ? #handle_endtag:處理結束標簽
? ? ? ? ? ? ? ? ? ? ? ? ? ? def handle_endtag(self,tag):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t = False #一個標簽讀取完成后恢復a_t變量為下一個標簽的讀取判斷做準備
#自定義類實例化
p = MYHTMLParser()
#訪問天氣預報官網
with request.urlopen("http://www.weather.com.cn/weather/101010100.shtml") as f:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data? = f.read().decode('utf-8')? ?#讀取網頁數據
#解析網頁信息查找未來七天的天氣情況并輸出到列表中
p.feed(str(data))
p.close() #關閉資源
#對列表進行操作輸出字典
L1 = list(list(L)[2:-6])[1:][::2]
L2 = list(list(L)[2:-6])[::2]
weather = dict(zip(L2,L1))
#輸出天氣
print(weather)
輸出結果如下圖:
源碼如下:
# 本模塊對一周天氣情況進行爬蟲
fromhtml.parserimportHTMLParser
fromurllibimportrequest
importre
# 自定義MYHTMLParser類
L = []
classMYHTMLParser(HTMLParser):
? ? ? ? ? ? ? ? ? ? ? ? ? a_t =False
? ? ? ? ? ? ? ? ? ? ? ? ? def handle_starttag(self,tag,attrs):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if tag =="h1"andlen(attrs) ==0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t =True
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? elif tag =="p"andlen(attrs) >1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t =True
? ? ? ? ? ? ? ? ? ? ? ? ? ?def handle_data(self,data):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ifself.a_t ==True:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?L.append(data)
? ? ? ? ? ? ? ? ? ? ? ? ? ?def? handle_endtag(self,tag):
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?self.a_t =False
# 自自定義類實例化
p = MYHTMLParser()
# 訪問天氣預報官網
withrequest.urlopen("http://www.weather.com.cn/weather/101010100.shtml")asf:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?data = f.read().decode('utf-8')
# 解析網頁輸出關鍵信息
p.feed(str(data))
p.close()
# 轉成字典
L1 =list(list(L)[2:-6])[1:][::2]
L2 =list(list(L)[2:-6])[::2]
weather =dict(zip(L2,L1))
print(weather)