import requests
import parsel
import csv
f = open('北京历史天气.csv',mode='w',encoding='utf-8',newline='')
csv_writer = csv.DictWriter(f,fieldnames=[
'日期',
'最高温',
'最低温',
'天气',
'风力风向',
'空气质量指数',
])
csv_writer.writeheader()
for year in range(2020,2023):
for month in range(1,13):
url = f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D=54511&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"Referer": "https://tianqi.2345.com/wea_history/54511.htm",
}
response = requests.get(url=url,headers=headers)
json_data = response.json() # .json()可以将拿到的text内容转为字典格式
html_data = json_data['data']
selector = parsel.Selector(html_data)
trs = selector.css('tr')
for tr in trs:
date = tr.css('td:nth-child(1)::text').get()
Max_temp = tr.css('td:nth-child(2)::text').get()
Lowest_temp = tr.css('td:nth-child(3)::text').get()
weather = tr.css('td:nth-child(4)::text').get()
wind = tr.css('td:nth-child(5)::text').get()
air_quality = tr.css('td:nth-child(6) span::text').get()
dit = {
'日期':date,
'最高温':Max_temp,
'最低温':Lowest_temp,
'天气':weather,
'风力风向':wind,
'空气质量指数':air_quality,
}
print(date,Max_temp,Lowest_temp,weather,wind,air_quality)
csv_writer.writerow(dit)
爬取北京2020到2022各月天气数据
最新推荐文章于 2024-09-01 00:00:00 发布