如何用python爬取天气预报,python爬虫爬取天气预报

大家好,小编来为大家解答以下问题,python爬虫爬取天气数据讲解,python爬取天气数据生成窗口,今天让我们一起来看看吧!

 目的:从天气网站中爬取数据,生成excel表格,里面存储南昌市近十一年的天气情况,并对爬取产生的数据进行数据分析。

第一步:编写代码进行数据爬取

首先,导入 requests 模块,并调用函数 requests.get(),从天气的网站上面获
取该函数所需要的各种参数,然后对里面的参数进行相应的赋值
其次,使用 pandas.concat().to_excel 函数,将爬取的结果保存到表格中,并
将其命名后保存到和代码文件相同的文件位置上

代码如下:

import pandas
import requests
url = 'https://tianqi.2345.com/Pc/GetHistory'
params = {'areaInfo[areaId]': 58606, 'areaInfo[areaType]': 2,
'date[year]': 2011,'date[month]': 1}
headers = {'user-agent': '''Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Edg/107.0.1418.62'''}
def craw_table(year,month):
params = {'areaInfo[areaId]': 58606, 'areaInfo[areaType]': 2,
'date[year]': year, 'date[month]': month}
pr = requests.get(url, headers=headers, params=params)
data = pr.json()['data']
df = pandas.read_html(data)[0]
return df
df_list=[]
for year in range(2011,2022):
for month in range(1,13):
print('爬取',year,month)
df = craw_table(year, month)
df_list.append(df)
#在py文件的文件夹中保存一个表格
pandas.concat(df_list).to_excel('南昌近十一年天气统计.xlsx',index=False)

结果展示:生成表格

表格内部数据形式:

 第二步:根据表格数据进行数据分析

代码展示:

import tkinter as tk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore')
#确保正确输出中文和负数
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 使用tkinter模板
root = tk.Tk()
root.title('南昌近十一年天气信息查询界面')
root.geometry('700x700')
#禁止修改窗口的大小
root.resizable(False, False)
# 设置标签
label1 = tk.Label(root, text='请选择想要获取何种的数据:', font=('楷书', 15))
label1.place(x=45, y=15)


def task_1():
    # 跳转到一个新的内容显示界面
    root1 = tk.Tk()
    root1.title('特定时间的天气查询')
    root1.geometry('700x700')
    root1.resizable(False, False)
    # 进行查询结果的展示
    label2 = tk.Label(root1, text='请输入日期,以 - 分隔:', font=('楷书', 15), background='white')
    label2.place(x=45, y=15)
    # 创建输入文本框
    text = tk.Text(root1, font=('宋体', 15))
    text.place(x=300, y=15, width=160, height=30)

    # 设置几个小按钮,用来点击查询和清空内容
    # 设置查询函数
    def get_data():
        # 获取文本,并进行缺失值的填充
        data1 = (pd.read_excel('南昌近十年天气统计.xlsx')).fillna('该年份没有统计该数据')
        # 获取文本内容
        str = (text.get("0.0", "end"))[0:10]
        for i in range(0, 4005):
            if str == data1.loc[i, '日期'][0:10]:
                put_data(data1.loc[i])

    def put_data(data):
        # 输出文本内容
        label2 = tk.Label(root1, text='查询结果为:', font=('楷书', 15), background='white')
        label2.place(x=45, y=150)
        text1 = tk.Text(root1, font=('宋体', 15))
        text1.place(x=200, y=150, width=320, height=250)
        text1.insert(index=tk.END, chars='时间:' + data[0])
        text1.insert(index=tk.END, chars='\n\n最高温:' + data[1] + 'C')
        text1.insert(index=tk.END, chars='\n\n最低温:' + data[2] + 'C')
        text1.insert(index=tk.END, chars='\n\n天气:' + data[3])
        text1.insert(index=tk.END, chars='\n\n风力风向:' + data[4])
        text1.insert(index=tk.END, chars='\n\n空气质量指数:' + data[5])

    # 设置查询按钮
    button1 = tk.Button(root1, text="查询", command=get_data, font=('楷书', 15))
    button1.place(x=200, y=60, width=50, height=50)

    # 设置清空函数
    def delete_text():
        text.delete(0.0, tk.END)

    # 设置清空按钮
    button2 = tk.Button(root1, text="清空", command=delete_text, font=('楷书', 15))
    button2.place(x=330, y=60, width=50, height=50)
    # 设置退出按钮
    button3 = tk.Button(root1, text="退出", command=root1.destroy, font=('楷书', 15))
    button3.place(x=450, y=60, width=50, height=50)
    # 显示界面
    root1.mainloop()


button1 = tk.Button(root, text="输入日期,显示当天天气的所有被记录的信息", command=task_1, font=('楷书', 15),
                    background='white')
button1.place(x=100, y=50, width=500, height=50)


def task_2():
    plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    data1 = pd.read_excel('南昌近十年天气统计.xlsx').fillna('该年份没有统计该数据')
    root1 = tk.Tk()
    root1.title('某个年份(2011-2021)中的各个月份的最高气温和最低气温')
    root1.geometry('700x700')
    root1.resizable(False, False)
    # 进行查询结果的展示
    label2 = tk.Label(root1, text='请输入年份(2011-2021):', font=('楷书', 15), background='white')
    label2.place(x=200, y=15)
    # 创建输入文本框
    text = tk.Text(root1, font=('宋体', 15))
    text.place(x=425, y=15, width=160, height=30)

    # 进行数据的处理
    def get_data():
        list1 = []
        list2 = []
        list_months = ['-01', '-02', '-03', '-04', '-05', '-06', '-07', '-08', '-09', '-10', '-11', '-12']
        # 寻找每一个月份
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值