OpenWeatherMap API Python – Access Current Weather Data Of Any Location

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.

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.

  • 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.
OpenWeatherMap API Python
OpenWeatherMap API Python

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.

  • 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.

OpenWeatherMap API Python
OpenWeatherMap API Python

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.

  • 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.
OpenWeatherMap API Python
OpenWeatherMap API Python

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.

  • Parameter zip is zip code of any place.

Result

  • Here i entered a zip code.
  • And country code is in(India).
OpenWeatherMap API Python
OpenWeatherMap API Python

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.

  • lat and lon are parameters that you have to pass.

Result

OpenWeatherMap API Python
OpenWeatherMap API Python

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 –

Result

OpenWeatherMap API Python
OpenWeatherMap API Python

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 –

And the result of this is –

OpenWeatherMap API Python
OpenWeatherMap API Python

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.

Result

OpenWeatherMap API Python
OpenWeatherMap API Python

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 –

Result

Now see the result of above program –

OpenWeatherMap API Python
OpenWeatherMap API Python

In this way you can access the current weather data using OpenWeatherMap API in python

Recommended articles :

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.

2 thoughts on “OpenWeatherMap API Python – Access Current Weather Data Of Any Location”

Leave a Comment