爬取需要的天气

该代码使用Python的requests库获取特定年月的天气历史数据,然后利用正则表达式提取高温和低温数据,将结果转换为整数列表。接着,通过pandas创建DataFrame,并将数据保存到CSV文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import re
import pandas as pd
import requests
import json

year = 2022
month = 5
url = f"https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=59289&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}"
response = requests.get(url)
json_dict = json.loads(response.text)
data_str = json_dict['data']
print(data_str)

re_linestring = re.compile(r'\s<td>.*')
re_high= re.compile(r'\s<td style="color:#ff5040;">.*')
re_lower= re.compile(r'\s<td style="color:#3097fd;" >.*')

high = re.findall(re_high, data_str)
lower = re.findall(re_lower, data_str)
print(high, lower)
high = [i.replace(' <td style="color:#ff5040;">', "").replace("°</td>", '') for i in high]
high = list(map(int, high))
print(high)

lower = [i.replace(' <td style="color:#3097fd;" >', "").replace("°</td>", '') for i in lower]
lower = list(map(int, lower))
print(lower)



years = re.findall(re_linestring, data_str)
years = [i.replace(" <td>", "") for i in years]
print(years)


years = [i.split(" ")[0] for i in years]
print(years)
years = [i for i in years if "-" in i]
print(years)


# print(len(years), len(high), len(lower))

df = pd.DataFrame({'years': years, 'high': high, 'lower': lower})
# df = pd.DataFrame({'years': years, 'high': high, 'lower': lower})
lis = []
for dic in df.to_dict("records"):
    for i in range(24):
        lis.append(dic)


new_df = pd.DataFrame(lis)
new_df.to_csv(f'{year}_{month}_new_df.csv', index=False)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值