Get Historical data from AccuWeather in Python

In this article, we will learn, how we can get the historical data of any location from the AccuWeather API portal and how to implement this API with the help of Python by making a small program.

Historical data from AccuWeather in Python

To understand this, you must have to go through the previous article in which I explain how to get the location key from the AccuWeather API with the help of Python. If you have not gone through this then please first visit that article before reading this article.

How to use AccuWeather API in Python – Full Guide

  • Note: In a free account, you can only get the previous 24 hr data of any particular location.

Step 1: We first create a function which is ‘pastdata’ in which we take the location key value.

def pastdata(location_key):

Step 2: After that, we paste the URL for the historical data via AccuWeather API.

past="http://dataservice.accuweather.com/currentconditions/v1/"+location_key+"/historical/24?apikey=-----------Your API Key---------------"
    print(past)

Step 3: With the help of ‘urllib.request’, we will open the URL and load our JSON data.

with urllib.request.urlopen(past) as past:
    data = json.loads(past.read().decode())
print(data)

Step 4: In the final step, with the help of for loop we will print our needed data for the last 24 hrs.

for key1 in data:
    print("Local Observation Date & Time "+key1['LocalObservationDateTime'])
    print("Weather " + key1['WeatherText'])
    print(" Is Day time: "+str(key1['IsDayTime']))
    print("Temperature: "+str(key1['Temperature']['Metric']['Value']))
    print("------------------------------------------------------------")

So our final code will be:

import json #since the key present in our JSON format so we use json library
import time
import urllib.request

API='-------Your API key-------------'
countrycode='IN'
city=input("Enter city name: ")

key=""
def getLocation(countrycode,city):
    search_address="http://dataservice.accuweather.com/locations/v1/cities/"+countrycode+"/search?apikey=-------Your API key-------------="+city+"&details=true"
    with urllib.request.urlopen(search_address) as search_address:
        data=json.loads(search_address.read().decode())
        loaction_key=data[0]['Key']
        return(loaction_key)

def pastdata(location_key):
    past="http://dataservice.accuweather.com/currentconditions/v1/"+location_key+"/historical/24?apikey=-------Your API key-------------"
    print(past)
    with urllib.request.urlopen(past) as past:
        data = json.loads(past.read().decode())
    print(data)
    for key1 in data:
        print("Local Observation Date & Time "+key1['LocalObservationDateTime'])
        print("Weather " + key1['WeatherText'])
        print(" Is Day time: "+str(key1['IsDayTime']))
        print("Temperature: "+str(key1['Temperature']['Metric']['Value']))
        print("------------------------------------------------------------")


key=getLocation(countrycode,city)
print(key)
pastdata(key)

The output will be:

Enter city name: Delhi
202396
http://dataservice.accuweather.com/currentconditions/v1/202396/historical/24?apikey=gsO707auRAAIKaIV3nggf7QPfNQj9TBY
[{'LocalObservationDateTime': '2022-10-23T20:28:00+05:30', 'EpochTime': 1666537080, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 26.8, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 80.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T19:28:00+05:30', 'EpochTime': 1666533480, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 28.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 83.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T18:28:00+05:30', 'EpochTime': 1666529880, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 29.8, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 86.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T17:28:00+05:30', 'EpochTime': 1666526280, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 31.4, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 89.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T16:33:00+05:30', 'EpochTime': 1666522980, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 31.4, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 89.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T15:27:00+05:30', 'EpochTime': 1666519020, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 33.7, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 93.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T14:28:00+05:30', 'EpochTime': 1666515480, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 32.5, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 90.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T13:28:00+05:30', 'EpochTime': 1666511880, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 31.0, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 88.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T12:28:00+05:30', 'EpochTime': 1666508280, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 29.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 85.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T11:28:00+05:30', 'EpochTime': 1666504680, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 26.3, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 79.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T10:28:00+05:30', 'EpochTime': 1666501080, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 24.7, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 76.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T09:28:00+05:30', 'EpochTime': 1666497480, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 22.9, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 73.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T08:27:00+05:30', 'EpochTime': 1666493820, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 21.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 70.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T07:27:00+05:30', 'EpochTime': 1666490220, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 20.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 68.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T06:28:00+05:30', 'EpochTime': 1666486680, 'WeatherText': 'Sunny', 'WeatherIcon': 1, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 19.8, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 68.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T05:28:00+05:30', 'EpochTime': 1666483080, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 18.7, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 66.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T04:28:00+05:30', 'EpochTime': 1666479480, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 64.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T03:34:00+05:30', 'EpochTime': 1666476240, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 17.6, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 64.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T02:34:00+05:30', 'EpochTime': 1666472640, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 21.0, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 70.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T01:35:00+05:30', 'EpochTime': 1666469100, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 22.3, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 72.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-23T00:27:00+05:30', 'EpochTime': 1666465020, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 23.0, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 73.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-22T23:27:00+05:30', 'EpochTime': 1666461420, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 23.8, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 75.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-22T22:28:00+05:30', 'EpochTime': 1666457880, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 25.0, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 77.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}, {'LocalObservationDateTime': '2022-10-22T21:28:00+05:30', 'EpochTime': 1666454280, 'WeatherText': 'Clear', 'WeatherIcon': 33, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': False, 'Temperature': {'Metric': {'Value': 26.0, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 79.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/delhi/202396/current-weather/202396?lang=en-us'}]
Local Observation Date & Time 2022-10-23T20:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 26.8
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T19:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 28.2
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T18:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 29.8
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T17:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 31.4
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T16:33:00+05:30
Weather Sunny
Is Day time: True
Temperature: 31.4
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T15:27:00+05:30
Weather Sunny
Is Day time: True
Temperature: 33.7
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T14:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 32.5
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T13:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 31.0
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T12:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 29.2
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T11:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 26.3
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T10:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 24.7
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T09:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 22.9
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T08:27:00+05:30
Weather Sunny
Is Day time: True
Temperature: 21.2
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T07:27:00+05:30
Weather Sunny
Is Day time: True
Temperature: 20.2
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T06:28:00+05:30
Weather Sunny
Is Day time: True
Temperature: 19.8
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T05:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 18.7
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T04:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 17.6
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T03:34:00+05:30
Weather Clear
Is Day time: False
Temperature: 17.6
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T02:34:00+05:30
Weather Clear
Is Day time: False
Temperature: 21.0
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T01:35:00+05:30
Weather Clear
Is Day time: False
Temperature: 22.3
------------------------------------------------------------
Local Observation Date & Time 2022-10-23T00:27:00+05:30
Weather Clear
Is Day time: False
Temperature: 23.0
------------------------------------------------------------
Local Observation Date & Time 2022-10-22T23:27:00+05:30
Weather Clear
Is Day time: False
Temperature: 23.8
------------------------------------------------------------
Local Observation Date & Time 2022-10-22T22:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 25.0
------------------------------------------------------------
Local Observation Date & Time 2022-10-22T21:28:00+05:30
Weather Clear
Is Day time: False
Temperature: 26.0
------------------------------------------------------------

So in this way, we can load our historical data from AccuWeather API using Python for any location.

I hope you like it

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *