注意:代码仅用于学习
相关网址:https://tianqi.2345.com/wea_history/57687/htm
查找url :
查找城市ID:
城市ID文件demo.js(JavaScript):
function get_city(){var provqx=new Array();provqx[10]=['58321-H 合肥-58321|71873-B 包河-58321|58326-C 巢湖-58321|70000-C 长丰-58321|60765-F 肥西-58321|70005-F 肥东-58321|71459-Y 渝中-57516|60295-Y 云阳-57516|60297-Y 永川-57516|60284-Z 忠县-57516']return provqx;
}
相关代码:
import requests
import parsel
import csv
import execjs#https://tianqi.2345.com/wea_history/57687/htm# GETALL = tr.xpath('./td//text()').getall()
f = open('demo.js',mode='r',encoding='utf-8').read()
ctx = execjs.compile(f)
# provqx = ctx.call("get_city")
city_list = ctx.call("get_city")
areaInfoList = []
for city_li in city_list:if city_li:for city_info in city_li:info_list = city_info.split('|')for info in info_list:city_code = info.split('-')[0]# city_code = citys.split('-')[1]# city_name = citys.split('-')[0]city_names = info.split('-')[1]city_name = city_names.split(' ')[1]areaInfoList.append([city_name,city_code])
# print(areaInfoList)## print(citys,citynames)with open('全国天气数据.csv',mode='a',encoding='utf-8',newline='') as f:csv.writer(f).writerow(['日期','最高温度','最低温度','天气','风力风向','空气质量','城市'])for areaInfo in areaInfoList:city_name = areaInfo[0]city_code = areaInfo[1]for year in range(2023,2024):for month in range(10,11):# csv_writer = csv.writer(f)# csv_writer.writerow(['日期','最高温度','最低温度','天气','风向','城市'])url = f'https://tianqi.2345.com/Pc/GetHistory?areaInfo%5BareaId%5D={city_code}&areaInfo%5BareaType%5D=2&date%5Byear%5D={year}&date%5Bmonth%5D={month}'response = requests.get(url=url)json_data = response.json()# print(json_data.get('data'))html_data = json_data.get('data')#select = parsel.Selector(html_data)trs = select.xpath('//tr')for tr in trs[1:]:# print(tr.xpath('string(td)').getall())# date = tr.xpath('string(td)').get()# print(tr.xpath('./td//text()').getall())tds = tr.xpath('./td//text()').getall()tds.append(city_name)print(tds)with open('全国天气数据.csv', mode='a', encoding='utf-8', newline='') as f:# csv_writer = csv.writer(f)# csv_writer.writerow(tds)csv.writer(f).writerow(tds)## #t clearfix## for tr in trs[1:]:# # tds = tr.css('td::text').getall()# # tds GETALL# print(tds)# # with open('河南12月天气数据.csv', mode='a', encoding='utf-8', newline='') as f:# # csv_writer = csv.writer(f)# # csv_writer.writerow(tds)