欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

話不多說,開始實操

正文 

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

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

f—string:

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

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

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

代碼:

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'  #網(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]    #通過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ù)字網(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]    #通過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ù)字網(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]    #通過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轉(zhuǎn)float類型
    rain_item.append(float(v))
print(d)

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

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

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

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

代碼:

#-*- 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'  #網(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]    #通過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ù)字網(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]    #通過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ù)字網(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]    #通過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)交互,調(diào)用編寫的get函數(shù)獲取對用戶輸入的內(nèi)容進行獲取并使用循環(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'  #網(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]    #通過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ù)字網(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]    #通過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ù)字網(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]    #通過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)中國天氣網(wǎng)有各省份降水量的數(shù)據(jù),可見我國在數(shù)據(jù)開源方面還有很長的路要走

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

相關文章

  • 利用Python實現(xiàn)最小二乘法與梯度下降算法

    利用Python實現(xiàn)最小二乘法與梯度下降算法

    這篇文章主要介紹了利用Python實現(xiàn)最小二乘法與梯度下降算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • PyQtGraph在pyqt中的應用及安裝過程

    PyQtGraph在pyqt中的應用及安裝過程

    這篇文章主要介紹了PyQtGraph在pyqt中的應用,文中給大家介紹了pyqtgraph的主要用途及PyQtGraph的安裝過程,需要的朋友可以參考下
    2019-08-08
  • 如何用Python做一個微信機器人自動拉群

    如何用Python做一個微信機器人自動拉群

    這篇文章主要介紹了如何用Python做一個微信機器人自動拉群,微當群人數(shù)達到100人后,用戶無法再通過掃描群二維碼加入,只能讓用戶先添加群內(nèi)聯(lián)系人微信,再由聯(lián)系人把用戶拉進來。這樣,聯(lián)系人員的私人微信會添加大量陌生人,給其帶來不必要的打擾,需要的朋友可以參考下
    2019-07-07
  • 詳解python的內(nèi)存分配機制

    詳解python的內(nèi)存分配機制

    Python的內(nèi)存分配機制是小白們最需要理解的概念之一。創(chuàng)建對象(變量、函數(shù)、對象等)后,CPython會在內(nèi)存中為其分配地址。Python有一個id()函數(shù),它可以返回對象的“身份”,也就是內(nèi)存地址。它實際上是一個唯一的整數(shù)。
    2021-05-05
  • Pycharm快速安裝OpenCV的詳細操作步驟

    Pycharm快速安裝OpenCV的詳細操作步驟

    Pycharm中使用OpenCV,其實也就是用Python語言調(diào)用OpenCV,下面這篇文章主要給大家介紹了關于Pycharm快速安裝OpenCV的詳細操作步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • 使用pandas實現(xiàn)篩選出指定列值所對應的行

    使用pandas實現(xiàn)篩選出指定列值所對應的行

    這篇文章主要介紹了使用pandas實現(xiàn)篩選出指定列值所對應的行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Django框架之路由用法

    Django框架之路由用法

    這篇文章介紹了Django框架之路由的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Python實例一個類背后發(fā)生了什么

    Python實例一個類背后發(fā)生了什么

    Python實例一個類背后發(fā)生了什么,本文為大家一一列出,感興趣的朋友可以參考一下
    2016-02-02
  • Python 列表的基本操作介紹

    Python 列表的基本操作介紹

    這篇文章主要介紹了Python 列表的基本操作,下面文章圍繞Python 列表的相關資料展開文章的詳細內(nèi)容,,需要的朋友可以參考一下,希望對大家有所幫助
    2021-11-11
  • Python實現(xiàn)從多表格中隨機抽取數(shù)據(jù)

    Python實現(xiàn)從多表格中隨機抽取數(shù)據(jù)

    這篇文章主要介紹了如何基于Python語言實現(xiàn)隨機從大量的Excel表格文件中選取一部分數(shù)據(jù),并將全部文件中隨機獲取的數(shù)據(jù)合并為一個新的Excel表格文件的方法,希望對大家有所幫助
    2023-05-05

最新評論