我是靠谱客的博主 怕孤独小蝴蝶,这篇文章主要介绍python爬取历史的天气数据,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import requests import demjson import csv # 构造2019全年的月份列表 months = [] for year in (2019,): for month in range(12): months.append("%d%02d"%(year, month+1)) todo_urls = [ "http://tianqi.2345.com/t/wea_history/js/"+month+"/58457_"+month+".js" for month in months ] datas = [] for url in todo_urls: r = requests.get(url,params=params) r.encoding = "gbk" if r.status_code!=200: raise Exception() # 去除javascript前后的字符串,得到一个js格式的JSON data = r.text.lstrip("var weather_str=").rstrip(";") datas.append(data) datas # 解析所有月份的数据 all_datas = [] for data in datas: tqInfos = demjson.decode(data)["tqInfo"] all_datas.extend([x for x in tqInfos if len(x)>0]) with open('hangzhou_tianqi_2019.csv', 'w', newline='', encoding='utf-8') as csv_file: writer = csv.writer(csv_file) columns = list(all_datas[0].keys()) writer.writerow(columns) for data in all_datas: writer.writerow([data[column] for column in columns])

最后

以上就是怕孤独小蝴蝶最近收集整理的关于python爬取历史的天气数据的全部内容,更多相关python爬取历史内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部