How to use AccuWeather API in Python – Full Guide
In this article, we will learn what AccuWeather API is, how we can create an account on that API portal and how to implement this API with the help of Python by making a small program.
Use AccuWeather API in Python
The AccuWeather Programming interface Engineer Entryway permits simple admittance to the most reliable, nitty gritty climate information to foster imaginative new weather conditions encounters.
AccuWeather gives hourly and Minute by Minute estimates with Prevalent Accuracy™ for any longitude/scope on The planet, with modified content and drawing in video introductions accessible on PDAs, tablets, free wired and versatile Web locales, associated televisions, and Web apparatuses, as well as through radio, TV, and papers.
To work with this API you need to first create a developer account so that you get the API key that will be used in the python program.
Steps to create an App and get its API key:
- Click on this website: https://developer.accuweather.com/
- Go to the registration page and enter the necessary details, after creating the account go to the login page and sign in with your account.
- go to the ‘API reference’ -> My Apps -> Add a new App
- Now enter your App name and choose the language python and click on Create App.
- Now from ‘The API reference click on Location API and Forecast API in the new tab.
- In ‘Location API” go to City Search (results narrowed by countryCode) and paste your API Key to get the key of the Selected City which will be used in our python program.
Now we will create a Python program that takes the city name from the user and predict the upcoming 5-day weather forecasting with the help of AccuWeather API.
Step 1: import necessary libraries:
import json #since the key present in our JSON format so we use json library import time import urllib.request
Step 2: Now write the API name and take city name from the user.
(Your API key: https://drive.google.com/file/d/1cQDE_dPck-0xBln5nKRwT4pLU6VBmdfs/view?usp=sharing )
API='gsO70###########################' #your API name countrycode='IN' city=input("Enter city name: ")
Step 3: Create a function name ‘getLocation(countrycode,city)’ which return the location key value.
key="" def getLocation(API,countrycode,city): search_address="http://dataservice.accuweather.com/locations/v1/cities/"+countrycode+"/search?apikey="+API+"&q="+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)
Step 4: Now Create a second function that forecasts the upcoming 5 days.
def getforecast(location_key): daily_forecastURL="http://dataservice.accuweather.com/forecasts/v1/daily/5day/"+location_key+"?apikey=-----------Your API KEY-------------------" print(daily_forecastURL) with urllib.request.urlopen(daily_forecastURL) as daily_forecastURL: data=json.loads(daily_forecastURL.read().decode()) print(data) for key1 in data["DailyForecasts"]: print("Weather Forecast for "+key1['Date']+"is:") print("Temperature in F (minimum) is: "+str(key1['Temperature']['Minimum']['Value'])) print("Temperature in F (maximum) is: " + str(key1['Temperature']['Maximum']['Value'])) print("Day Forecast "+str(key1['Day']['IconPhrase'])) print("------------------------------------------------------------------------")
Here, we first open the URL and then load the required data of JSON and after that, we will take the required values one by one and print the forecast for the upcoming 5 days.
Also read: OpenWeatherMap Tutorial in Python using PyOWM
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='API key value' countrycode='IN' city=input("Enter city name: ") key="" def getLocation(countrycode,city): search_address="http://dataservice.accuweather.com/locations/v1/cities/"+countrycode+"/search?apikey=----------------------------------="+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 getforecast(location_key): daily_forecastURL="http://dataservice.accuweather.com/forecasts/v1/daily/5day/"+location_key+"?apikey=------------------------" print(daily_forecastURL) with urllib.request.urlopen(daily_forecastURL) as daily_forecastURL: data=json.loads(daily_forecastURL.read().decode()) print(data) for key1 in data["DailyForecasts"]: print("Weather Forecast for "+key1['Date']+"is:") print("Temperature in F (minimum) is: "+str(key1['Temperature']['Minimum']['Value'])) print("Temperature in F (maximum) is: " + str(key1['Temperature']['Maximum']['Value'])) print("Day Forecast "+str(key1['Day']['IconPhrase'])) print("------------------------------------------------------------------------") key=getLocation(countrycode,city) print(key) getforecast(key)
The output will be:
Enter city name: Faridabad 202446 http://dataservice.accuweather.com/forecasts/v1/daily/5day/202446?apikey=-------------------------------- {'Headline': {'EffectiveDate': '2022-10-22T07:00:00+05:30', 'EffectiveEpochDate': 1666402200, 'Severity': 7, 'Text': 'Hazy sunshine this weekend', 'Category': '', 'EndDate': None, 'EndEpochDate': None, 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?lang=en-us'}, 'DailyForecasts': [{'Date': '2022-10-20T07:00:00+05:30', 'EpochDate': 1666229400, 'Temperature': {'Minimum': {'Value': 67.0, 'Unit': 'F', 'UnitType': 18}, 'Maximum': {'Value': 89.0, 'Unit': 'F', 'UnitType': 18}}, 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=1&lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=1&lang=en-us'}, {'Date': '2022-10-21T07:00:00+05:30', 'EpochDate': 1666315800, 'Temperature': {'Minimum': {'Value': 66.0, 'Unit': 'F', 'UnitType': 18}, 'Maximum': {'Value': 91.0, 'Unit': 'F', 'UnitType': 18}}, 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=2&lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=2&lang=en-us'}, {'Date': '2022-10-22T07:00:00+05:30', 'EpochDate': 1666402200, 'Temperature': {'Minimum': {'Value': 62.0, 'Unit': 'F', 'UnitType': 18}, 'Maximum': {'Value': 90.0, 'Unit': 'F', 'UnitType': 18}}, 'Day': {'Icon': 5, 'IconPhrase': 'Hazy sunshine', 'HasPrecipitation': False}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=3&lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=3&lang=en-us'}, {'Date': '2022-10-23T07:00:00+05:30', 'EpochDate': 1666488600, 'Temperature': {'Minimum': {'Value': 62.0, 'Unit': 'F', 'UnitType': 18}, 'Maximum': {'Value': 89.0, 'Unit': 'F', 'UnitType': 18}}, 'Day': {'Icon': 5, 'IconPhrase': 'Hazy sunshine', 'HasPrecipitation': False}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=4&lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=4&lang=en-us'}, {'Date': '2022-10-24T07:00:00+05:30', 'EpochDate': 1666575000, 'Temperature': {'Minimum': {'Value': 61.0, 'Unit': 'F', 'UnitType': 18}, 'Maximum': {'Value': 90.0, 'Unit': 'F', 'UnitType': 18}}, 'Day': {'Icon': 1, 'IconPhrase': 'Sunny', 'HasPrecipitation': False}, 'Night': {'Icon': 33, 'IconPhrase': 'Clear', 'HasPrecipitation': False}, 'Sources': ['AccuWeather'], 'MobileLink': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=5&lang=en-us', 'Link': 'http://www.accuweather.com/en/in/faridabad/202446/daily-weather-forecast/202446?day=5&lang=en-us'}]} Weather Forecast for 2022-10-20T07:00:00+05:30is: Temperature in F (minimum) is: 67.0 Temperature in F (maximum) is: 89.0 Day Forecast Sunny ------------------------------------------------------------------------ Weather Forecast for 2022-10-21T07:00:00+05:30is: Temperature in F (minimum) is: 66.0 Temperature in F (maximum) is: 91.0 Day Forecast Sunny ------------------------------------------------------------------------ Weather Forecast for 2022-10-22T07:00:00+05:30is: Temperature in F (minimum) is: 62.0 Temperature in F (maximum) is: 90.0 Day Forecast Hazy sunshine ------------------------------------------------------------------------ Weather Forecast for 2022-10-23T07:00:00+05:30is: Temperature in F (minimum) is: 62.0 Temperature in F (maximum) is: 89.0 Day Forecast Hazy sunshine ------------------------------------------------------------------------ Weather Forecast for 2022-10-24T07:00:00+05:30is: Temperature in F (minimum) is: 61.0 Temperature in F (maximum) is: 90.0 Day Forecast Sunny ------------------------------------------------------------------------ Process finished with exit code 0
So in this way, we use the AccuWeather API in Python. You can also add or predict the different values as per your need using Python.
Leave a Reply