python爬取各省降水量及可視化詳解
在具體數(shù)據(jù)的選取上,我爬取的是各省份降水量實(shí)時(shí)數(shù)據(jù)
話不多說(shuō),開始實(shí)操
正文
1.爬取數(shù)據(jù)
- 使用python爬蟲,爬取中國(guó)天氣網(wǎng)各省份24時(shí)整點(diǎn)氣象數(shù)據(jù)
- 由于降水量為動(dòng)態(tài)數(shù)據(jù),以js形式進(jìn)行存儲(chǔ),故采用selenium方法經(jīng)xpath爬取數(shù)據(jù)—ps:在進(jìn)行數(shù)據(jù)爬取時(shí),最初使用的方法是漂亮湯法(beautifulsoup)法,但當(dāng)輸出爬取的內(nèi)容(<class = split>時(shí),卻空空如也。在源代碼界面Ctrl+Shift+F搜索后也無(wú)法找到降水量,后查詢得知此為動(dòng)態(tài)數(shù)據(jù),無(wú)法用該方法進(jìn)行爬取
- 使用循環(huán)和分類的方式爬取省份不同、網(wǎng)址相似的降水量數(shù)據(jù),順帶記錄數(shù)據(jù)對(duì)應(yīng)的城市
f—string:
url_a= f'http://www.weather.com.cn/weather1dn/101{a}0101.shtml'
f-string 用大括號(hào) {} 表示被替換字段,其中直接填入替換內(nèi)容
將城市和降水量相對(duì)應(yīng)后存入字典再打印
代碼:
from lxml import etree from selenium import webdriver import re city = [''for n in range(34)] #存放城市列表 rain = [''for n in range(34)] #存放有關(guān)降雨量信息的數(shù)值 rain_item = [] driver = webdriver.Chrome(executable_path='chromedriver') #使用chrome瀏覽器打開 for a in range(1,5): #直轄市數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0100.shtml' #網(wǎng)址 driver.get(url_a) #打開網(wǎng)址 rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(5,10): #一位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(10,35): #二位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/101{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 d = dict(zip(city,rain)) #將城市和降水量的列表合成為字典 for k,v in d.items(): #str轉(zhuǎn)float類型 rain_item.append(float(v)) print(d)
在對(duì)爬取的內(nèi)容進(jìn)行處理時(shí),可能會(huì)因?yàn)閿?shù)據(jù)的類型而報(bào)錯(cuò),如爬下來(lái)的數(shù)據(jù)為str類型,而排序需要數(shù)字類型,故需要進(jìn)行float類型轉(zhuǎn)化
使用該爬取方法,是模擬用戶打開網(wǎng)頁(yè),并且會(huì)在電腦上進(jìn)行顯示。在爬取實(shí)驗(yàn)進(jìn)行中途,中國(guó)天氣網(wǎng)進(jìn)行了網(wǎng)址更新,原網(wǎng)址出現(xiàn)了部分城市數(shù)據(jù)無(wú)法顯示的問(wèn)題,但當(dāng)刷新界面后,數(shù)據(jù)可正常顯示,此時(shí)可采用模擬鼠標(biāo)點(diǎn)擊刷新的方法避免錯(cuò)誤。由于后續(xù)找到了新網(wǎng)址,故將這一方法省去。
2.數(shù)據(jù)可視化
- 用Matplotlib庫(kù)函數(shù)繪制曲線,并輸出最大值及相應(yīng)城市、最小值及相應(yīng)城市、平均值和中位值
- 數(shù)據(jù)的確定:medium分奇偶計(jì)算中位值所處排序后數(shù)據(jù)的位置(中位值);用sum求和后除于數(shù)據(jù)個(gè)數(shù)(平均值);max和min函數(shù)找到最值再由數(shù)值經(jīng)循環(huán)找到對(duì)應(yīng)的城市列表
- 繪圖:使用plt函數(shù)繪制圖像,并注明橫縱坐標(biāo)、所需注釋
- 文本處理:在進(jìn)行注釋時(shí),plt函數(shù)所要求的格式為str類型,故需要進(jìn)行類型轉(zhuǎn)換,同時(shí)添加適當(dāng)文字說(shuō)明
代碼:
#-*- codeing = utf-8 -*- import matplotlib.pyplot as plt from lxml import etree from selenium import webdriver import re import matplotlib matplotlib.rc("font",family='YouYuan') city = [''for n in range(34)] #存放城市列表 rain = [''for n in range(34)] #存放有關(guān)降雨量信息的數(shù)值 driver = webdriver.Chrome(executable_path='chromedriver') #使用chrome瀏覽器打開 for a in range(1,5): #直轄市數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0100.shtml' #網(wǎng)址 driver.get(url_a) #打開網(wǎng)址 rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(5,10): #非直轄一位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(10,35): #非直轄二位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/101{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 if len(rain)%2 == 0: #尋找中值 medium = int(len(rain)/2) else: medium = int(len(rain)/2)+1 medium_text = "中位值:" + rain[medium] d = dict(zip(city,rain)) #將城市和降水量的列表合成為字典 rain_item = [] city_min = [] city_max = [] for k,v in d.items(): rain_item.append(float(v)) average_rain = sum(rain_item)/len(rain_item) average_text = "平均值:"+ str(average_rain) max_rain = max(rain_item) #最大值 min_rain = min(rain_item) #最小值 for k,v in d.items(): if float(v) == min_rain: city_min.append(k) min_text = "降雨量最小的城市:"+str(city_min)+" 最小值:"+str(min_rain) for k,v in d.items(): if float(v) ==max_rain: city_max.append(k) max_text = "降雨量最大的城市:"+str(city_max)+" 最大值:"+str(max_rain) plt.bar(range(len(d)), rain_item, align='center') plt.xticks(range(len(d)), list(d.keys())) plt.xlabel('城市',fontsize=20) plt.ylabel('降水量',fontsize=20) plt.text(0,12,average_text,fontsize=6) plt.text(0,13,medium_text,fontsize=6) plt.text(0,14,max_text,fontsize=6) plt.text(0,15,min_text,fontsize=6) plt.show()
2.運(yùn)行界面
3.互動(dòng)界面
使用tkinter庫(kù)進(jìn)行GUI的構(gòu)建使用button函數(shù)實(shí)現(xiàn)交互,調(diào)用編寫的get函數(shù)獲取對(duì)用戶輸入的內(nèi)容進(jìn)行獲取并使用循環(huán)進(jìn)行遍歷處理,若城市輸入正確,則在界面上輸出當(dāng)?shù)氐慕邓看a:
#-*- codeing = utf-8 -*- from lxml import etree from selenium import webdriver import re import matplotlib matplotlib.rc("font",family='YouYuan') from tkinter import * import tkinter as tk city = [''for n in range(34)] #存放城市列表 rain = [''for n in range(34)] #存放有關(guān)降雨量信息的數(shù)值 driver = webdriver.Chrome(executable_path='chromedriver') #使用chrome瀏覽器打開 for a in range(1,5): #直轄市數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0100.shtml' #網(wǎng)址 driver.get(url_a) #打開網(wǎng)址 rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(5,10): #非直轄一位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/1010{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 for a in range(10,35): #非直轄二位數(shù)字網(wǎng)址數(shù)據(jù) url_a= f'http://www.weather.com.cn/weather1dn/101{a}0101.shtml' driver.get(url_a) rain_list = [] city_list = [] resp_text = driver.page_source page_html = etree.HTML(resp_text) city_list = page_html.xpath('/html/body/div[4]/div[2]/a')[0] #通過(guò)xpath爬取城市名稱 rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0] #通過(guò)xpath爬取降雨量數(shù)據(jù) city[a-1] = city_list.text #存入城市列表 rain[a-1] = re.findall(r"\d+\.?\d*",rain_list.text)[0] #存入數(shù)值 d = dict(zip(city,rain)) #將城市和降水量的列表合成為字典 root=tk.Tk() root.title('降水量查詢') root.geometry('500x200') def get(): values = entry.get() for k,v in d.items(): if k == values: label = Label(root, text= v+'mm') label.pack() frame = Frame(root) frame.pack() u1 = tk.StringVar() entry = tk.Entry(frame, width=20, textvariable=u1, relief="sunken") entry.pack(side="left") frame1 = Frame(root) frame1.pack() btn1=Button(frame1, text="查詢", width=20, height=1, relief=GROOVE, command=lambda :get()) btn1.pack(side="left") root.mainloop()
4.運(yùn)行界面
寫在最后
在爬取天氣的過(guò)程中,僅發(fā)現(xiàn)中國(guó)天氣網(wǎng)有各省份降水量的數(shù)據(jù),可見我國(guó)在數(shù)據(jù)開源方面還有很長(zhǎng)的路要走
到此這篇關(guān)于python爬取各省降水量及可視化詳解的文章就介紹到這了,更多相關(guān)python爬取請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
- Python編寫可視化界面的全過(guò)程(Python+PyCharm+PyQt)
- Python實(shí)現(xiàn)K-means聚類算法并可視化生成動(dòng)圖步驟詳解
- python數(shù)據(jù)分析之員工個(gè)人信息可視化
- 關(guān)于Python可視化Dash工具之plotly基本圖形示例詳解
- python用pyecharts實(shí)現(xiàn)地圖數(shù)據(jù)可視化
- Python繪制K線圖之可視化神器pyecharts的使用
- Python繪制詞云圖之可視化神器pyecharts的方法
- python 可視化庫(kù)PyG2Plot的使用
- Python數(shù)據(jù)分析之彩票的歷史數(shù)據(jù)
- Python 數(shù)據(jù)分析之逐塊讀取文本的實(shí)現(xiàn)
- Python數(shù)據(jù)分析庫(kù)pandas高級(jí)接口dt的使用詳解
- Python Pandas數(shù)據(jù)分析工具用法實(shí)例
- 用Python 爬取貓眼電影數(shù)據(jù)分析《無(wú)名之輩》
- 大數(shù)據(jù)分析用java還是Python
- python 數(shù)據(jù)分析實(shí)現(xiàn)長(zhǎng)寬格式的轉(zhuǎn)換
- PyCharm設(shè)置Ipython交互環(huán)境和宏快捷鍵進(jìn)行數(shù)據(jù)分析圖文詳解
- Python實(shí)戰(zhàn)之疫苗研發(fā)情況可視化
相關(guān)文章
利用Python實(shí)現(xiàn)最小二乘法與梯度下降算法
這篇文章主要介紹了利用Python實(shí)現(xiàn)最小二乘法與梯度下降算法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02PyQtGraph在pyqt中的應(yīng)用及安裝過(guò)程
這篇文章主要介紹了PyQtGraph在pyqt中的應(yīng)用,文中給大家介紹了pyqtgraph的主要用途及PyQtGraph的安裝過(guò)程,需要的朋友可以參考下2019-08-08如何用Python做一個(gè)微信機(jī)器人自動(dòng)拉群
這篇文章主要介紹了如何用Python做一個(gè)微信機(jī)器人自動(dòng)拉群,微當(dāng)群人數(shù)達(dá)到100人后,用戶無(wú)法再通過(guò)掃描群二維碼加入,只能讓用戶先添加群內(nèi)聯(lián)系人微信,再由聯(lián)系人把用戶拉進(jìn)來(lái)。這樣,聯(lián)系人員的私人微信會(huì)添加大量陌生人,給其帶來(lái)不必要的打擾,需要的朋友可以參考下2019-07-07使用pandas實(shí)現(xiàn)篩選出指定列值所對(duì)應(yīng)的行
這篇文章主要介紹了使用pandas實(shí)現(xiàn)篩選出指定列值所對(duì)應(yīng)的行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Python實(shí)例一個(gè)類背后發(fā)生了什么
Python實(shí)例一個(gè)類背后發(fā)生了什么,本文為大家一一列出,感興趣的朋友可以參考一下2016-02-02Python實(shí)現(xiàn)從多表格中隨機(jī)抽取數(shù)據(jù)
這篇文章主要介紹了如何基于Python語(yǔ)言實(shí)現(xiàn)隨機(jī)從大量的Excel表格文件中選取一部分?jǐn)?shù)據(jù),并將全部文件中隨機(jī)獲取的數(shù)據(jù)合并為一個(gè)新的Excel表格文件的方法,希望對(duì)大家有所幫助2023-05-05