我正在尝试创建一个从API检索的5年天气数据集
city=input("enter\n")
ask=input("enter date\n")
date_format = "%Y-%m-%d"
date_time = datetime.strptime(ask, date_format)
print(date_time)
json_data = requests.get(
'http://api.worldweatheronline.com/premium/v1/past-weather.ashx',
params=dict(
key='my key',
q=city,
format='json',
date=ask,
tp='24'
)
).json()
print(json_data)
它显示0ne天数据。 但是我需要从Api收集5年的数据,所以我使用
channels = ['maxtempC','maxtempF', 'mintempC','mintempF']
channels_list = []
for channel in channels:
JSONContent = requests.get("http://api.worldweatheronline.com/premium/v1/past-weather.ashx" + channel).json()
channels_list.append([JSONContent['Type'], JSONContent['Temp'], JSONContent['mintem'],JSONContent['views']])
dataset = pd.DataFrame(channels_list)
dataset.sample(5)
给出
JSONDecodeError: Expecting value: line 1 column 1 (char 0)
我如何从Api收集5年数据?
答案 0 :(得分:2)
在不了解API内部信息的情况下,我建议在5年内每天进行一次每日调用,并将每日数据附加到列表中:
import datetime
import requests
定义一个函数,该函数将在给定的起始日期和结束日期之间的所有日期返回生成器(而不是列表),必须将其输入为日期时间对象:
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
示例值:
city="London"
start_date="2019-03-01"
end_date="2019-03-11"
date_format = "%Y-%m-%d"
start_date = datetime.datetime.strptime(start_date, date_format)
end_date = datetime.datetime.strptime(end_date, date_format)
每天从开始日期到结束日期收集数据,每天一个请求:
all_data = []
maxtempC_list = []
maxtempF_list = []
mintempC_list = []
mintempF_list = []
for each_date in daterange(start_date, end_date):
print(each_date.date())
ask = str(each_date.date())
json_data = requests.get(
'http://api.worldweatheronline.com/premium/v1/past-weather.ashx',
params=dict(
key='YOUR_API_KEY',
q=city,
format='json',
date=ask,
tp='24'
)
).json()
print(json_data)
all_data.append(json_data)
maxtempC_list.append(json_data['data']['weather'][0]['maxtempC'])
maxtempF_list.append(json_data['data']['weather'][0]['maxtempF'])
mintempC_list.append(json_data['data']['weather'][0]['mintempC'])
mintempF_list.append(json_data['data']['weather'][0]['mintempF'])