Python爬蟲(chóng)爬取杭州24時(shí)溫度并展示操作示例
本文實(shí)例講述了Python爬蟲(chóng)爬取杭州24時(shí)溫度并展示操作。分享給大家供大家參考,具體如下:
散點(diǎn)圖 爬蟲(chóng)杭州今日24時(shí)溫度 https://www.baidutianqi.com/today/58457.htm
利用正則表達(dá)式爬取杭州溫度 面向?qū)ο缶幊? 圖表展示(散點(diǎn)圖 / 折線圖)導(dǎo)入相關(guān)庫(kù)
import requestsimport refrom matplotlib import pyplot as pltfrom matplotlib import font_managerimport matplotlib
類代碼部分
class Weather(object): def __init__(self): self.url = ’https://www.baidutianqi.com/today/58457.htm’ self.headers = {’user-agent’: ’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36’} #請(qǐng)求 def __to_requests(self): response = requests.get(url=self.url,headers=self.headers) return self.__to_paeser(response.content.decode(’utf-8’)) #解析 def __to_paeser(self,html): #正則表達(dá)式 要從數(shù)據(jù)循環(huán)的部分寫起 如果從循環(huán)的父標(biāo)簽開(kāi)始 , 則只會(huì)匹配到一個(gè)值 即父標(biāo)簽下的某個(gè)標(biāo)簽 , 而不是循環(huán)下的 pattern = re.compile(’<li>.*?<font class='red'>(.*?)</font>.*?<font class='blue'>(.*?)</font></li>’,re.S) return re.findall(pattern,html) #展示 def __to_show(self,data): x = [] y = [] for value in data: x.append(value[0]) y.append(int(value[1][-2:])) #畫(huà)布 plt.figure(figsize=(15,8),dpi=80) #中文 /System/Library/Fonts/PingFang.ttc C:WindowsFontssimsun.ttc my_font = font_manager.FontProperties(fname=’/System/Library/Fonts/PingFang.ttc’,size=18) #x y 軸刻度 標(biāo)簽 區(qū)分 y的刻度值/刻度標(biāo)簽 和 y本身的值 plt.xticks(fontproperties=my_font,rotation=60) y_ticks = ['{}℃'.format(i) for i in range(min(y),max(y)+1)] plt.yticks(range(min(y),max(y)+1),y_ticks,fontproperties=my_font,rotation=60) # x y 軸說(shuō)明 plt.xlabel(’時(shí)間’,color=’orange’,rotation=60,fontproperties=my_font) plt.ylabel(’溫度’,color=’orange’,rotation=60,fontproperties=my_font) #網(wǎng)格 plt.grid(alpha=0.4) #標(biāo)題 plt.title(’當(dāng)天時(shí)刻溫度低值變化’,fontproperties=my_font) #圖例 plt.legend(prop=my_font) #作畫(huà)# plt.scatter(x,y,label=’2019-08-22’) plt.plot(x,y,color=’red’) plt.show() #操作 def to_run(self): result = self.__to_requests() self.__to_show(result)
調(diào)用并展示
if __name__ == ’__main__’: wt = Weather() wt.to_run()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析2. Hybris在idea中debug配置方法詳解3. Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法4. 在idea中為注釋標(biāo)記作者日期操作5. jsp cookie+session實(shí)現(xiàn)簡(jiǎn)易自動(dòng)登錄6. .NET Core Web APi類庫(kù)內(nèi)嵌運(yùn)行的方法7. .NET6使用ImageSharp實(shí)現(xiàn)給圖片添加水印8. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車9. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)10. .net如何優(yōu)雅的使用EFCore實(shí)例詳解
