Maryam Anwer
Maryam Anwer

Reputation: 43

Creating a data set using an API with Python

I'm trying to create a 5 years weather data set retrieved from an API

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)

it show 0ne day data. but i need to collect 5 years data from Api so i use

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)

it gives

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

How i collect 5 years data from Api?

Upvotes: 0

Views: 675

Answers (1)

Zulfiqaar
Zulfiqaar

Reputation: 633

without knowing the internals of the API, i can suggest performing a daily call for each day in the 5 years, and appending the daily data to a list:

import datetime
import requests

defining a function that will return a generator (not a list) for all the days between a given start_date and end_date, which must be inputted as datetime objects:

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + datetime.timedelta(n)

example values:

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)

collect daily data from start_date to end_date, one request per day:

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'])

Upvotes: 3

Related Questions