Get Weather data from OpenWeatherMap using Python


Hello, today's weather was certainly confusing. When I got up in the morning, it was cold. When I was going to college, there was sunny weather. I do not know what's going on.

So what I think adding one functionality to my Personal Assistant project "Lisa" so that she can tell me about today's weather at morning.

SoI started searching for a weather site which can give me an accurate result and have good API support and I came across the OpenWeatherMap site which I want.

So after reading it's API documents I registered on that site and got my KEY. and write the following programme which gives me accurate results of current weather.

Now every morning Lisa informs me today's weather.

import requests

api_address = 'http://api.openweathermap.org/data/2.5/weather?appid=PASTE_YOUR_KEY_HERE&q='
city = input("Name of city : ")

url = api_address + city

json_data = requests.get(url).json()
weatherDescription = json_data['weather'][0]['description']
weatherTemp = str(round(((json_data['main']['temp'])-273.15), 2))
weatherHumidity = str(json_data['main']['humidity'])
weatherPressure = str(json_data['main']['pressure'])
weatherWind = str(json_data['wind']['speed'])
weatherWindDegree = str(json_data['wind']['deg'])

print("as per the data collected weather of " +city+ " is " + weatherDescription)
print("The temperature is " +weatherTemp+ " °Celsius")
print("Wind will be flowing " +weatherWind+ " meter per seconds to the " + weatherWindDegree+ " degree")
print("Humidity is " +weatherHumidity+ " %")
print("and the pressure is about " +weatherPressure+ " hpa")

This is an example of getting data from OpenWeatherMap. Result is in JSON format so we can easily extract info from the results.



NOTE: Before using above code you should get the key from OpenWeatherMap by registering your account and paste it in above code at line no 3.
Previous PostOlder Post Home

0 comments:

Post a Comment