利用Python中pandas库的read_html功能从网站查历史天气 (q-weather.info)获取历史气象数据,并使用tkinter库实现窗口可视化。
代码如下:
1.首先导入必要的库:
import tkinter as tk
from tkinter import messagebox
import pandas as pd
2.定义一个用法,使用户可以查看所有气象基准站(截至2018年,来源:2018年 中国基准、基本气象站 站点信息_昆明市气象站点站号-CSDN博客)
def namelist():
error_label.config(text="")
messagebox.showinfo("基站名录",'#这里写入基站名录')
3.为方便用户使用,提供根据城市查询站点的功能:同样是定义一个用法,从台站编号.xlsx中获取相关信息,同时如果出现错误,显示错误信息。(文件附文章末)
def sake():
error_label.config(text="")
def sss():
error_label.config(text="")
city_name = city_name_entry.get()
try:
df = pd.read_excel('台站编号.xlsx')
name = df.loc[df['city'] == city_name, 'name']
if not name.empty:
message_str = "\n".join(f"{index}: {value}" for index, value in name.items())
messagebox.showinfo("成功",message_str)
else:
messagebox.showinfo("失败", '未匹配到相应站点')
except Exception as e:
error_label.config(text=str(e))
root1 = tk.Tk()
root1.title("城市对应台站查询")
tk.Label(root1, text="请输入城市名字:").grid(row=0, column=0)
city_name_entry = tk.Entry(root1)
city_name_entry.grid(row=0, column=1)
query_button3 = tk.Button(root1, text="查询", command=sss)
query_button3.grid(row=2, column=0)
4.实现获取数据的功能,也是定义一个用法:
def get_station_info():
error_label.config(text="")
input_name = station_name_entry.get()
date_time = date_time_entry.get()
try:
df = pd.read_excel('台站编号.xlsx')
print_number = df.loc[df['name'] == input_name, 'number']
if not print_number.empty:
number = print_number.item()
url = 'https://q-weather.info/weather/' + str(number) + '/history/?date=' + str(date_time)
messagebox.showinfo("成功", "台站信息已处理,正在输出数据,请稍后...(点击确认继续)")
tables_df = pd.DataFrame()
tables_df = tables_df._append(pd.read_html(url))
tables_df.to_csv(input_name + date_time + '.csv', encoding='gbk')
df_str = tables_df.to_string()
messagebox.showinfo("成功", df_str)
messagebox.showinfo("成功", "目标数据已输出到//气象数据获取//文件夹中")
else:
error_label.config(text="未找到匹配的台站名称。")
except Exception as e:
error_label.config(text=str(e))
5.最后,实现窗口的可视化,并可让用户在窗口中输入相关信息,通过三按钮链接到三个用法:
root = tk.Tk()
root.title("台站信息查询")
tk.Label(root, text="请输入台站名称:").grid(row=0, column=0)
station_name_entry = tk.Entry(root)
station_name_entry.grid(row=0, column=1)
tk.Label(root, text="请输入时间(格式如YYYY-MM-DD): ").grid(row=1, column=0)
date_time_entry = tk.Entry(root)
date_time_entry.grid(row=1, column=1)
query_button = tk.Button(root, text="查询", command=get_station_info)
query_button1 = tk.Button(root, text="基站名录", command=namelist)
query_button2 = tk.Button(root, text="基站查询", command=sake)
query_button.grid(row=2, column=1)
query_button1.grid(row=2, column=0)
query_button2.grid(row=2, column=2)
error_label = tk.Label(root, text="", fg="red")
error_label.grid(row=3, column=1)
root.mainloop()
6.完整代码:
#encoding=UTF-8
import tkinter as tk
from tkinter import messagebox
import pandas as pd
def namelist():
error_label.config(text="")
messagebox.showinfo("基站名录","#这里写入基站名录")
def sake():
error_label.config(text="")
def sss():
error_label.config(text="")
city_name = city_name_entry.get()
try:
df = pd.read_excel('台站编号.xlsx')
name = df.loc[df['city'] == city_name, 'name']
if not name.empty:
message_str = "\n".join(f"{index}: {value}" for index, value in name.items())
messagebox.showinfo("成功",message_str)
else:
messagebox.showinfo("失败", '未匹配到相应站点')
except Exception as e:
error_label.config(text=str(e))
root1 = tk.Tk()
root1.title("城市对应台站查询")
tk.Label(root1, text="请输入城市名字:").grid(row=0, column=0)
city_name_entry = tk.Entry(root1)
city_name_entry.grid(row=0, column=1)
query_button3 = tk.Button(root1, text="查询", command=sss)
query_button3.grid(row=2, column=0)
def get_station_info():
error_label.config(text="")
input_name = station_name_entry.get()
date_time = date_time_entry.get()
try:
df = pd.read_excel('台站编号.xlsx')
print_number = df.loc[df['name'] == input_name, 'number']
if not print_number.empty:
number = print_number.item()
url = 'https://q-weather.info/weather/' + str(number) + '/history/?date=' + str(date_time)
messagebox.showinfo("成功", "台站信息已处理,正在输出数据,请稍后...(点击确认继续)")
tables_df = pd.DataFrame()
tables_df = tables_df._append(pd.read_html(url))
tables_df.to_csv(input_name + date_time + '.csv', encoding='gbk')
df_str = tables_df.to_string()
messagebox.showinfo("成功", df_str)
messagebox.showinfo("成功", "目标数据已输出到//气象数据获取//文件夹中")
else:
error_label.config(text="未找到匹配的台站名称。")
except Exception as e:
error_label.config(text=str(e))
root = tk.Tk()
root.title("台站信息查询")
tk.Label(root, text="请输入台站名称:").grid(row=0, column=0)
station_name_entry = tk.Entry(root)
station_name_entry.grid(row=0, column=1)
tk.Label(root, text="请输入时间(格式如YYYY-MM-DD): ").grid(row=1, column=0)
date_time_entry = tk.Entry(root)
date_time_entry.grid(row=1, column=1)
query_button = tk.Button(root, text="查询", command=get_station_info)
query_button1 = tk.Button(root, text="基站名录", command=namelist)
query_button2 = tk.Button(root, text="基站查询", command=sake)
query_button.grid(row=2, column=1)
query_button1.grid(row=2, column=0)
query_button2.grid(row=2, column=2)
error_label = tk.Label(root, text="", fg="red")
error_label.grid(row=3, column=1)
root.mainloop()
7.运行效果:
可以通过点击‘基站名录’查看所有基站,点击基站查询:
输入城市名称查询相应站点:
输入站点名称和时间查询数据:
点击查询:
相关数据会保存到文件夹中:
第一次写文章,代码有不足之处还请各位大佬多多指正
台站信息参考文章2018年 中国基准、基本气象站 站点信息_昆明市气象站点站号-CSDN博客
保留台站编号、台站名称和城市,分别命名为number,name,city
标签:name,Python,text,获取,grid,tk,df,row,气象 From: https://blog.csdn.net/nan_xi_/article/details/139760283