How to get Weather of any city using Open Weather API |Python
How to get Weather of any city using Open Weather API
We can easily get the weather details of any city by just typing on any browser. Also, there are many applications provided for us to know the weather of any city. All these are possible just because of APIs and smartphones with built-in GPS, we have access to mobile apps that provide an hour-by-hour forecast. But, some services allow us to regularly download current weather and forecast data in JSON and CSV format. There is no need to call an API to do this. These APIs provide several bulk files with current weather and forecasts. It is available for more than 209k cities.
There are several Weather API's available to access Global Weather Data. Some of the most popular weather API's are:
- OpenWeatherMap API: for weather Forecast.
- Weatherbit API: for weather forecasts and alerts.
- AccuWeather API: for weather conditions, images, cyclones, and more.
- Dark Sky API: for forecast and historical data.
- Weather2020 API: for long-range weather forecast
- ClimaCell API: for a realtime, short time and hourly forecasts
Through this article, we are going to create an application that will use the 'OpenWeatherMap' API to fetch the weather of any city. So to connect API with the project, we need an API key to authenticate our request. So, first, need to create an account on 'OpenWeatherMap',
then we can get copy your API key and use it in the code.
- Request: This module helps us to send the HTTP request using python. This request returns a response object with all the response data like content, encoding, status, etc.
So using pip install this module before using it.
- JSON: It is a built-in package in python, which can be used t work with JSON data. It is also a syntax for storing and exchanging the data. We can easily convert the python objects like int, list, dict, float, True, False string, tuple, etc into JSON
Here, we are using this python's package because the OpenWeatherMap API provides the weather data into some formats
like JSON and XML. We are preferring JSON over XML because JSON is more readable, has a compact style, and even it provides faster software parsing.
#importing modules import requests,json #type your API KEY Here. api_key = "your APIKey" base_url = "https://api.openweathermap.org/data/2.5/weather?" #taking input "city name" from user city_name = input("Enter the city name: ") #complete_url variable to store the complete_url address complete_url= base_url + "appid" + api_key + "&q=" +city_name #get methods of requests module retruns respons object response = requests.get(complete_url) #json method of response object convert json format data into python format data x = response.json() #Now x contains list of nested dictionaries #check the value of "cod" key is equal to "404", means city is found otherwise, city is not found if x["cod"] != "404": #store the value of "main" key in variable y y = x["main"] #store the value coressponding to the "temp" key of y current_temperature = y["temp"] #store the value coressponding to the "pressure" key of y current_pressure =y["pressure"] #store the value coressponding to the "humidity" key of y current_humidity =y["humidity"] #store the value of "weather" key in variable z z= x["weather"] #store the value coressponding to the "description" key #at the 0th index of z weather_description = z["description"] #print the following values print(" Temperature(in kelvin unit)= " + str(current_temperature) + "\n atmospheric pressure (in hPa unit) = " + str(current_pressure) + "\n humidity (in percantage) = " + str(current_humidity) + "\n description = " + str(weather_description)) else: print("City Not Found")
If you find this article useful then stay tuned with us for more interesting articles on Python.