In this tutorial, we will find the city’s weather using the OpenWeatherMap API. To use the OpenWeatherMap API, we need to get the API key. We will get it by creating an account on their website.
Create an account and get your API key. Free up to 60 calls per minute. You have to pay if you want more than that. In this tutorial, the free version is enough. We need an HTTP application module and a JSON module to work with feedback. Follow the steps below in the weather for any city.
- Submit JSON applications and modules.
- Generate a basic weather API URL https://api.openweathermap.org/data/2.5/weather ?.
- Launch city and API key.
- Update basic URL with API key and city name.
- Submit a recovery request using the applications.get () method.
- Also extract weather information using the JSON module in response.
Example
Let’s see the code.
import urllib.request as url
import json
path = "https://api.openweathermap.org/data/2.5/weather lat=28.6139&lon=77.2090&appid=[API_ID]"
response = url.urlopen(path)
response
<http.client.HTTPResponse at 0x2b3bf7b5780>
data = json.load(response)
data.keys()
dict_keys(['coord', 'weather', 'base', 'main', 'visibility', 'wind', 'clouds', 'dt', 'sys', 'timezone', 'id', 'name', 'cod'])
data.values()
dict_values([{'lon': 77.2134, 'lat': 28.6105}, [{'id': 721, 'main': 'Haze', 'description': 'haze', 'icon': '50d'}], 'stations', {'temp': 296.24, 'feels_like': 295.6, 'temp_min': 296.24, 'temp_max': 296.24, 'pressure': 1009, 'humidity': 38}, 3000, {'speed': 3.6, 'deg': 300}, {'all': 0}, 1645012943, {'type': 1, 'id': 9165, 'country': 'IN', 'sunrise': 1644974945, 'sunset': 1645015300}, 19800, 1260877, 'Parliament House, Delhi', 200])
data['main']
{'temp': 296.24, 'feels_like': 295.6, 'temp_min': 296.24, 'temp_max': 296.24, 'pressure': 1009, 'humidity': 38}
Output
By running the above program, you will get the following results.
data['main']['temp']
296.24
Conclusion
If you find any difficulty in following the tutorial, mention them in the comment section.
Important Notice for college students
If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com
For more Programming related blogs Visit Us Geekycodes . Follow us on Instagram.