import requests
import parsel
import csv
f = open('天气.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.writer(f)
csv_writer.writerow(['日期', '最高温度', '最低温度', '天气', '风向','空气质量指数', '城市'])
city_list = [54511, 58362, 59287, 59493]
for city in city_list:
city_name = ''
if city == 54511:
city_name = '北京'
elif city == 58362:
city_name = '上海'
elif city == 59287:
city_name = '广州'
elif city == 59493:
city_name = '深圳'
for year in range(2013, 2023):
for month in range(1, 13):
url = f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'
response = requests.get(url=url)
html_data = response.json()['data']
select = parsel.Selector(html_data)
trs = select.css('.history-table tr')
for tr in trs[1:]:
tds = tr.css('td::text').getall()
tds.append(city_name)
print(tds)
csv_writer.writerow(tds)
为什么爬取不了2345天气网中空气质量数据?代码如下
最新推荐文章于 2024-08-22 07:51:42 发布