Python Weather API [closed]
Asked Answered
B

1

40

How do I import weather data into a Python program?

Bernadine answered 24/9, 2009 at 22:19 Comment(0)
Q
70

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

Quota answered 27/10, 2012 at 12:47 Comment(5)
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/pyowmEladiaelaeoptene
mmm Interesting. How do I print the speed from {u'speed': 3.1, u'deg': 220}. @paoloChkalov
@user2763992 the speed is in the dictionary returned by get_wind. Try print w.get_wind()['speed']Quota
what about the api limitation requests? how many requests/day can be done?Dichroite
Hello - do you know if its possible to 'loop' this code so that you can always get the latest up to date weather ?Tenatenable

© 2022 - 2024 — McMap. All rights reserved.