Reading Time:- 2 min 32 sec
Reading Time:- 2 min 32 sec
BY TEJAS
In this article, we are going to create a weather info program using Python and OpenWeatherMap API. Which will tell you real-time weather info.
Weather Info using Python & OpenWeatherMap
openweathermap is a tool that offers weather data, including current weather data, forecasts, and historical data to the developers of web services and mobile applications.
So let's start the process.
ADVERTISEMENT
First of all, you will need the API key of OpenWeatherMap for which you have to create your account on the website opeanweathemap.org. You can also click on the direct https://home.openweathermap.org/users/sign_up link to sign up.
Now when you reach the signup page, you need to fill and submit the signup form when that is done, you will get an account activation link on the email you provide which you have to click on. When that happens, your account will be successfully activated.
Once the account is activated you have to click on the API keys tab which will take you to the API keys page where you will see your default API key. You can also use it or you can create a new key using the Create key option below.
ADVERTISEMENT
Now your first step is complete. In which we found our API key.
Let us now move on to our second step in which we will create our python program.
First, we will import two modules requests
and json
.
After importing modules now we are going to create six variables first variable is the city
in which we are going to store the name of the city we want. The second variable is api_key
in which the openweathermap API key is stored. Now, for the third time, we will store our basic URL in the variable base_url
. Fourthly, we will create a variable url
that will store the proper URL whose structure will be base_url + "appid =" + api_key + "& q =" + city
. Now for the fifth time, we will store requests.get(url)
in the response variable which will request your URL.
The last variable x
is going to store the response received from API. We will get the response in JSON format.
After that, we will only print the values we have met.
The full Python program is as follows.
ADVERTISEMENT
import requests, json # Importing Modules
city = "Mumbai" # City Name
api_key = "#Your API key paste here" #API Key received from openweathermap.org
base_url = "https://api.openweathermap.org/data/2.5/weather?" # Base URL Address
url = base_url + "appid=" + api_key + "&q=" + city # Complete URL address
response = requests.get(url) # Return response object
x = response.json()
# print (x)
if x['cod'] != 401:
print("City Name: ", x['name']) # print city name
print("Weather: ", x['weather'][0]['main']) # print weather
print("Temp in Kelvin: ", x['main']['temp']) # print temperature in Kelvin
print("Minimum Temp in Kelvin: ", x['main']['temp_min']) # print minimum temperature in Kelvin
print("Maximum Temp in Kelvin: ", x['main']['temp_max']) # print Maximum temperature in Kelvin
else:
print("City Not Found") # if city is not found
COMMENTS