Python Weather API

99,159

Since Google has shut down its weather API, I suggest to check out OpenWeatherMap:

The OpenWeatherMap service provides free weather data and forecast API suitable for any cartographic services like web and smartphones applications. Ideology is inspired by OpenStreetMap and Wikipedia that make information free and available for everybody. OpenWeatherMap provides wide range of weather data such as map with current weather, week forecast, precipitation, wind, clouds, data from weather Stations and many others. Weather data is received from global Meteorological broadcast services and more than 40 000 weather stations.

It's not a Python library, but it's super easy to use, because you can get results in JSON format.

Here's an example using Requests:

>>> from pprint import pprint
>>> import requests
>>> r = requests.get('http://api.openweathermap.org/data/2.5/weather?q=London&APPID={APIKEY}')
>>> pprint(r.json())
{u'base': u'cmc stations',
 u'clouds': {u'all': 68},
 u'cod': 200,
 u'coord': {u'lat': 51.50853, u'lon': -0.12574},
 u'dt': 1383907026,
 u'id': 2643743,
 u'main': {u'grnd_level': 1007.77,
           u'humidity': 97,
           u'pressure': 1007.77,
           u'sea_level': 1017.97,
           u'temp': 282.241,
           u'temp_max': 282.241,
           u'temp_min': 282.241},
 u'name': u'London',
 u'sys': {u'country': u'GB', u'sunrise': 1383894458, u'sunset': 1383927657},
 u'weather': [{u'description': u'broken clouds',
               u'icon': u'04d',
               u'id': 803,
               u'main': u'Clouds'}],
 u'wind': {u'deg': 158.5, u'speed': 2.36}}

And here's an example using PyOWM, a Python wrapper around the OpenWeatherMap web API:

>>> import pyowm
>>> owm = pyowm.OWM()
>>> observation = owm.weather_at_place('London,uk')
>>> w = observation.get_weather()
>>> w.get_wind()
{u'speed': 3.1, u'deg': 220}
>>> w.get_humidity()
76

The official API documentation is available here.

To get the API key sign up to open weather map here

Share:
99,159
Steven
Author by

Steven

...

Updated on March 11, 2020

Comments

  • Steven
    Steven about 4 years

    How do I import weather data into a Python program?

  • csparpa
    csparpa over 9 years
    OpenWeatherMap web API resources are formatted using different JSON blobs depending on the endpoints. So, parsing is terrible... Avoid all of this hassle and don't reinvent the wheel by using an external library - eg: PyOWM github.com/csparpa/pyowm
  • Paolo Moretti
    Paolo Moretti over 9 years
    @csparpa Thanks, I updated the answer!
  • Yoweli Kachala
    Yoweli Kachala over 8 years
    mmm Interesting. How do I print the speed from {u'speed': 3.1, u'deg': 220}. @paolo
  • Paolo Moretti
    Paolo Moretti over 8 years
    @user2763992 the speed is in the dictionary returned by get_wind. Try print w.get_wind()['speed']
  • enneppi
    enneppi over 6 years
    what about the api limitation requests? how many requests/day can be done?
  • Sjharrison
    Sjharrison about 5 years
    Hello - do you know if its possible to 'loop' this code so that you can always get the latest up to date weather ?