Welcome to OpenWeatherMap API Python tutorial. Using openweathermap API, you can access current weather of any location, weather forecasting, historical data, weather stations etc. I am pretty sure, you will really enjoy this tutorial. So follow this till the end.
Contents
- 1 OpenWeatherMap API Python Tutorial – Getting Started With OpenWeatherMap API
- 2 OpenWeatherMap API Python Tutorial -Access Current Weather Data For One Location
- 3 OpenWeatherMap API Python Tutorial -Access Current Weather Data For Several Cities
- 4 OpenWeatherMap API Python Tutorial -Access Desired Current Weather Data
- 5 Recommended articles :
OpenWeatherMap API Python Tutorial – Getting Started With OpenWeatherMap API
We can access weather data using openweathermap API. So give a quick look on openweathermap API –
openweathermap API
- It is a fast and easy-to-work weather APIs.
- The main benefit of this API is that you can use it free.
- It access current weather data for any location on Earth including over 200,000 cities!
- Current weather is frequently updated based on global models and data from more than 40,000 weather stations.
- In openweathermap API data is available in JSON, XML, or HTML format.
- OpenWeatherMap provides many kinds of weather maps including Precipitation, Clouds, Pressure, Temperature, Wind. You can connect them to mobile and web apps.
- It was founded in 2012 and its founder is Extreme Electronics LTD.
openweathermap API Signup Process
To use openweathermap API, you need to create an account in their site.
- Go to openweathermap and signup yourself.
- Then you will get an API key.
Getting openweathermap Data
For getting openweathermap data, you have to pass the following keywords in url of your browser.
1 2 3 |
http://api.openweathermap.org/data/2.5/weather?appid=YOUR API KEY&q=Delhi |
- Here i have passed two parameters, one is appid i.e., your API key and another one is q i.e., city name.
- So we get the following weather data in JSON format.
So now you can see all the weather data of a city has been accessed.
Now we will see how to access weather data in python using openweathermap API.
OpenWeatherMap API Python Tutorial -Access Current Weather Data For One Location
Now in this section, we will see how to access weather data. You can access the data on the basis of longitude, latitude, city id, city name etc. So let’s start doing it.
Accessing Data By City Id
You can access weather data by city id. This will give you exact result
- You can download list of city ID from this link . In this link you will see many files but you have to download city.list.json.gz .
- And now write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import requests from pprint import pprint # API KEY API_key = "Your API key" # This stores the url base_url = "http://api.openweathermap.org/data/2.5/weather?" # This will ask the user to enter city ID city_id = input("Enter a city ID : ") # This is final url. This is concatenation of base_url, API_key and city_id Final_url = base_url + "appid=" + API_key + "&id=" + city_id # this variable contain the JSON data which the API returns weather_data = requests.get(Final_url).json() # JSON data is difficult to visualize, so you need to pretty print pprint(weather_data) |
- Here i have pretty printed the data, because JSON data is very difficult to read and that’s why it should be pretty printed.
- For more details about pretty print JSON data check this.
Python JSON Pretty Print – JSON Formatting with Python
Result
So now let’s see the result. In the result you can see the complete weather data of city ID 2172797.
Accessing Data By City Name
Now we will access the weather information by passing city name. You can access data by passing city name or city name and country code. So let’s see how to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import requests from pprint import pprint API_key = "Your API key" base_url = "http://api.openweathermap.org/data/2.5/weather?" city_name = input("Enter a city Name : ") Final_url = base_url + "appid=" + API_key + "&q=" + city_name weather_data = requests.get(Final_url).json() print("\nCurrent Weather Data Of " + city_name +":\n") pprint(weather_data) |
- Parameter q is city name. You can also pass city name and country code.
So let’s see the result –
- Here i am accessing the weather data of Delhi city which is in India.
Accessing Data By Zip Code
So now, if you want to access weather data according to Zip code then you can do so in very easy way. So let’s see how to do that –
- To access data by zip code, you have to pass zip code and country code parameters.
- If you don’t pass country code then search works for USA as a default.
- And now write the following code snippets and run them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import requests from pprint import pprint API_key = "Your API key" base_url = "http://api.openweathermap.org/data/2.5/weather?" zip_code = input("Enter a Zip code and country code : ") Final_url = base_url + "appid=" + API_key + "&zip=" + zip_code weather_data = requests.get(Final_url).json() print("\nCurrent Weather Data Of " + zip_code +":\n") pprint(weather_data) |
- Parameter zip is zip code of any place.
Result
- Here i entered a zip code.
- And country code is in(India).
Accessing Data By Geographic Coordinates
You can also access the weather data by geographic coordinates.
- Geographic coordinates means either of the two lines of latitude and longitude whose intersection determines the geographical point of a place.
- So you can find the weather data by passing the longitude or latitude of a particular location.
- Let’s see how to do it in python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import requests from pprint import pprint API_key = "Your API Key" base_url = "http://api.openweathermap.org/data/2.5/weather?" latitude = input("Enter Latitude : ") longitude = input("Enter Longitude : ") Final_url = base_url + "appid=" + API_key + "&lat=" + latitude + "&lon=" + longitude weather_data = requests.get(Final_url).json() print("\nWeather Data By Geograhic Coordinates :" ) pprint(weather_data) |
- lat and lon are parameters that you have to pass.
Result
OpenWeatherMap API Python Tutorial -Access Current Weather Data For Several Cities
In this section you will learn how to access weather data for several cities. That means accessing data for –
- Cities in cycle
- Cities within a rectangle zone
- Call for several city IDs
So let’s see all of them in python.
Accessing Data For Cities In Cycle
Using openweathermap, you can also access weather data for cities in cycle. In this, JSON returns data from cities laid within definite circle that is specified by center point (‘lat’, ‘lon’) and expected number of cities (‘cnt’) around this point.
- You can access data of maximum 50 cities.
- And 10 is default number of cities.
Parameters
You can specify following parameters –
- lat – latitude
- lon – longitude
- callback – functionName for JSONP callback.
- cluster – use server clustering of points. Possible values are [yes, no]
- lang – language [en , ru … ]
- cnt – number of cities around the point that should be returned
Now let’s see it with an example, so write the following code –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import requests from pprint import pprint API_key = "Your API Key" base_url = "http://api.openweathermap.org/data/2.5/find?" latitude = str(input("Enter Latitude : ")) longitude = str(input("Enter Longitude : ")) no_of_cities = str(input("Enter No of cities : ")) Final_url = base_url + "appid=" + API_key + "&lat=" + latitude +"&lon=" + longitude + "&cnt=" + no_of_cities weather_data = requests.get(Final_url).json() print("\nWeather Data of Cities In Cycle :\n") pprint(weather_data) |
Result
Accessing Data For Cities Within A Rectangle Zone
You can also access weather data of cities within a rectangle zone. That means JSON returns the data from cities within the defined rectangle specified by the geographic coordinates.
Parameters:
- bbox bounding box [lon-left,lat-bottom,lon-right,lat-top,zoom]
- callback javascript functionName
- cluster use server clustering of points. Possible values are [yes, no]
- lang language [ru, en … ]
Let’s implement it practically in python –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import requests from pprint import pprint API_key = "Your API Key" base_url = "http://api.openweathermap.org/data/2.5/box/city?" bbox = input("Enter Bounding Box : ") Final_url = base_url + "appid=" + API_key + "&bbox=" +bbox weather_data = requests.get(Final_url).json() print("\nWeather Data of " + bbox + " Rectangle zone :\n") pprint(weather_data) |
And the result of this is –
Accessing Data For Several City IDs
You can access data of several cities simultaneously by calling several city IDs. That means you can pass more than one city ID at one time.
- You can only call upto 20 city IDs at one time.
- XML and HTML formats are not available for several city IDs, you can only get data in JSON format
Let’s do it practically in python. So write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import requests from pprint import pprint API_key = "Your API Key" base_url = "http://api.openweathermap.org/data/2.5/group?" cities_id = input("Enter city IDs : ") Final_url = base_url + "appid=" + API_key + "&id=" +cities_id weather_data = requests.get(Final_url).json() print("\nWeather Data of City IDs :" + cities_id ) pprint(weather_data) |
Result
OpenWeatherMap API Python Tutorial -Access Desired Current Weather Data
You can also get the data according to your requirement instead of getting whole data. This is quite easy, so let’s do it practically in python. So write the following code –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import requests API_key = "Your API Key" base_url = "http://api.openweathermap.org/data/2.5/weather?" city_name = input("Enter City Name : ") Final_url = base_url + "appid=" + API_key + "&q=" + city_name weather_data = requests.get(Final_url).json() # JSON data works just similar to python dictionary and you can access the value using []. # Accessing Temperature, temperature resides in main and its key is temp temp = weather_data['main']['temp'] # Accessing wind speed, it resides in wind and its key is speed wind_speed = weather_data['wind']['speed'] # Accessing Description, it resides in weather and its key is description description = weather_data['weather'][0]['description'] # Accessing Latitude, it resides in coord and its key is lat latitude = weather_data['coord']['lat'] # Accessing Longitude, it resides in coord and its key is lon longitude = weather_data['coord']['lon'] # Printing Data print('\nTemperature : ',temp) print('\nWind Speed : ',wind_speed) print('\nDescription : ',description) print('\nLatitude : ',latitude) print('\nLongitude : ',longitude) |
Result
Now see the result of above program –
In this way you can access the current weather data using OpenWeatherMap API in python
Recommended articles :
- Wikipedia API Python – Scrapping Wikipedia With Python
- Python Turtle Module – A Complete Guide For Creating Graphics In Python
- Run Python On Android – How To Run Python Programs On Android
- Python Rest API Example using Bottle Framework
- Python Browser Automation Using Selenium
So this was all about OpenWeatherMap API Python tutorial. I hope you found it helpful so share this with others. And if you have any query regarding this then leave your comment. Thanks everyone.
Such a Great Article!! I learned something new from your blog. Amazing stuff. I would like to follow your blog frequently. Keep Rocking!!