我是靠谱客的博主 飘逸玫瑰,最近开发中收集的这篇文章主要介绍python爬取历史天气查询_历史天气爬取,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

历史天气爬取

爬取来源:2345天气网

爬取周期:月度,可自行更改

爬取城市:部分城市,可自行更改

具体代码:

import requests

import demjson

import csv

list = [

‘71398’,

‘60169’,

‘70912’,

‘60901’,

‘70924’,

‘60554’,

‘60495’,

‘60400’,

‘58633’,

‘60181’,

‘71013’,

‘70353’,

‘60083’,

‘60498’,

‘60841’,

‘60900’,

‘60705’,

‘58927’,

‘60899’,

‘70034’,

‘60552’,

‘60715’,

‘60890’,

‘60854’,

‘60551’,

‘60181’,

‘60366’,

‘60654’,

‘71401’,

‘61021’,

‘70145’,

‘60597’,

‘60563’,

‘60189’,

‘61042’,

‘60116’,

‘60181’,

‘60181’,

‘70471’,

‘71409’,

‘71011’,

‘60116’,

‘60761’,

‘60191’,

‘60591’,

‘70328’,

‘61081’,

‘61027’,

‘60708’,

‘58549’,

‘71391’,

‘60886’,

‘71149’,

‘60415’,

‘60995’,

‘70479’,

‘70324’,

‘60080’,

‘60084’,

‘60807’,

‘71149’,

‘60709’,

‘71149’,

‘60928’,

‘70332’,

‘60406’,

‘71400’

]

url_list = [f"http://tianqi.2345.com/t/wea_history/js/202012/{xx}_202012.js" for xx in list]

datas = []

for url in url_list:

r = requests.get(url)

if r.status_code != 200:

raise Exception()

data = r.text.lstrip(“var weather_str =”).rstrip(";")

datas.append(data)

demjson.decode(datas[0])[“tqInfo”]

yearTq = []

for month in datas:

tq = demjson.decode(month)[“tqInfo”]

yearTq.extend([n for n in tq if len(n) != 0])

with open("./历史天气爬取.csv", “w”, newline="", encoding=“utf-8”) as csv_file:

Weather_file = csv.writer(csv_file)

title = yearTq[0].keys()

Weather_file.writerow(title)

for data in yearTq:

Weather_file.writerow([data[column] for column in title])

有啥问题可以咨询我,V:DL994845967,希望大家共同学习

最后

以上就是飘逸玫瑰为你收集整理的python爬取历史天气查询_历史天气爬取的全部内容,希望文章能够帮你解决python爬取历史天气查询_历史天气爬取所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部