最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python爬取各省降水量及可視化詳解

 更新時間:2021年04月16日 15:07:20   作者:天Ye浪Sir  
本文是學習python,故選取了python最常用的爬蟲作為實操訓練同時,還添加了可視化和GUI入門的內容使爬取的內容應用更豐富,需要的朋友可以參考下

在具體數(shù)據(jù)的選取上,我爬取的是各省份降水量實時數(shù)據(jù)

話不多說,開始實操

正文 

1.爬取數(shù)據(jù)

  • 使用python爬蟲,爬取中國天氣網各省份24時整點氣象數(shù)據(jù)
  • 由于降水量為動態(tài)數(shù)據(jù),以js形式進行存儲,故采用selenium方法經xpath爬取數(shù)據(jù)—ps:在進行數(shù)據(jù)爬取時,最初使用的方法是漂亮湯法(beautifulsoup)法,但當輸出爬取的內容(<class = split>時,卻空空如也。在源代碼界面Ctrl+Shift+F搜索后也無法找到降水量,后查詢得知此為動態(tài)數(shù)據(jù),無法用該方法進行爬取
  • 使用循環(huán)和分類的方式爬取省份不同、網址相似的降水量數(shù)據(jù),順帶記錄數(shù)據(jù)對應的城市

f—string:

url_a= f'http://www.weather.com.cn/weather1dn/101{a}0101.shtml'

f-string 用大括號 {} 表示被替換字段,其中直接填入替換內容

將城市和降水量相對應后存入字典再打印

代碼:

from lxml import etree
from selenium import webdriver
import re
city = [''for n in range(34)]   #存放城市列表
rain = [''for n in range(34)]   #存放有關降雨量信息的數(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'  #網址
    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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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轉float類型
    rain_item.append(float(v))
print(d)

在對爬取的內容進行處理時,可能會因為數(shù)據(jù)的類型而報錯,如爬下來的數(shù)據(jù)為str類型,而排序需要數(shù)字類型,故需要進行float類型轉化

使用該爬取方法,是模擬用戶打開網頁,并且會在電腦上進行顯示。在爬取實驗進行中途,中國天氣網進行了網址更新,原網址出現(xiàn)了部分城市數(shù)據(jù)無法顯示的問題,但當刷新界面后,數(shù)據(jù)可正常顯示,此時可采用模擬鼠標點擊刷新的方法避免錯誤。由于后續(xù)找到了新網址,故將這一方法省去。

2.數(shù)據(jù)可視化

  • 用Matplotlib庫函數(shù)繪制曲線,并輸出最大值及相應城市、最小值及相應城市、平均值和中位值
  • 數(shù)據(jù)的確定:medium分奇偶計算中位值所處排序后數(shù)據(jù)的位置(中位值);用sum求和后除于數(shù)據(jù)個數(shù)(平均值);max和min函數(shù)找到最值再由數(shù)值經循環(huán)找到對應的城市列表
  • 繪圖:使用plt函數(shù)繪制圖像,并注明橫縱坐標、所需注釋
  • 文本處理:在進行注釋時,plt函數(shù)所要求的格式為str類型,故需要進行類型轉換,同時添加適當文字說明

代碼:

#-*- 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)]   #存放有關降雨量信息的數(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'  #網址
    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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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.運行界面

在這里插入圖片描述

3.互動界面

使用tkinter庫進行GUI的構建使用button函數(shù)實現(xiàn)交互,調用編寫的get函數(shù)獲取對用戶輸入的內容進行獲取并使用循環(huán)進行遍歷處理,若城市輸入正確,則在界面上輸出當?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)]   #存放有關降雨量信息的數(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'  #網址
    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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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ù)字網址數(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]    #通過xpath爬取城市名稱
    rain_list = page_html.xpath('//*[@id="weatherChart"]/div[2]/p[5]')[0]   #通過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.運行界面

在這里插入圖片描述 

寫在最后

在爬取天氣的過程中,僅發(fā)現(xiàn)中國天氣網有各省份降水量的數(shù)據(jù),可見我國在數(shù)據(jù)開源方面還有很長的路要走

到此這篇關于python爬取各省降水量及可視化詳解的文章就介紹到這了,更多相關python爬取請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持腳本之家!

相關文章

  • pandas取出重復數(shù)據(jù)的方法

    pandas取出重復數(shù)據(jù)的方法

    今天小編就為大家分享一篇pandas取出重復數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python中str.join()簡單用法示例

    Python中str.join()簡單用法示例

    這篇文章主要介紹了Python中str.join()簡單用法,結合實例形式分析了Python中str.join()用于連接生成新字符串的相關操作技巧,需要的朋友可以參考下
    2018-03-03
  • Python中SOAP項目的介紹及其在web開發(fā)中的應用

    Python中SOAP項目的介紹及其在web開發(fā)中的應用

    這篇文章主要介紹了Python中的SOAP項目及其在web開發(fā)中的應用,本文來自于IBM官方網站技術文檔,需要的朋友可以參考下
    2015-04-04
  • Python如何使用ConfigParser讀取配置文件

    Python如何使用ConfigParser讀取配置文件

    這篇文章主要介紹了Python如何使用ConfigParser讀取配置文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • python高級特性和高階函數(shù)及使用詳解

    python高級特性和高階函數(shù)及使用詳解

    Python很棒,它有很多高級用法值得細細思索,學習使用。這篇文章主要介紹了python高級特性和高階函數(shù)及使用詳解,需要的朋友可以參考下
    2018-10-10
  • Linux永久修改pip配置源的詳細過程

    Linux永久修改pip配置源的詳細過程

    默認情況下pip使用的是國外的鏡像,在下載的時候速度非常慢,所以需要更換PIP的鏡像源,下面這篇文章主要給大家介紹了關于Linux永久修改pip配置源的相關資料,需要的朋友可以參考下
    2024-02-02
  • 一文搞懂python異常處理、模塊與包

    一文搞懂python異常處理、模塊與包

    異常就是系統(tǒng)中的錯誤,程序是無法改變和處理的,文中有給大家提到異常處理機制,模塊與包的相關知識,通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-06-06
  • Python?獲取指定開頭指定結尾所夾中間內容(推薦)

    Python?獲取指定開頭指定結尾所夾中間內容(推薦)

    獲取文章中指定開頭、指定結尾中所夾的內容。其中,開頭和結尾均有多種,但最多也就十幾種,所以代碼還是具有可行性的,今天小編給大家介紹通過Python?獲取指定開頭指定結尾所夾中間內容,感興趣的朋友一起看看吧
    2023-02-02
  • 使用Python生成跑馬燈視頻的完整代碼

    使用Python生成跑馬燈視頻的完整代碼

    這篇文章主要介紹了如何使用Python生成跑馬燈視頻,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-11-11
  • python編程-將Python程序轉化為可執(zhí)行程序[整理]

    python編程-將Python程序轉化為可執(zhí)行程序[整理]

    python編程-將Python程序轉化為可執(zhí)行程序[整理]...
    2007-04-04

最新評論

临沂市| 徐汇区| 永城市| 荔波县| 丰都县| 阿尔山市| 海林市| 舞阳县| 方山县| 明溪县| 巴青县| 游戏| 西青区| 祁东县| 广西| 车致| 梁山县| 北宁市| 克什克腾旗| 资阳市| 邳州市| 阿合奇县| 南京市| 平湖市| 游戏| 邯郸市| 尚义县| 电白县| 疏附县| 金沙县| 福建省| 金乡县| 玉溪市| 伊川县| 花垣县| 梁平县| 仲巴县| 孟津县| 阿坝县| 万全县| 汨罗市|