2009-09-24 23 views
23

気象データをPythonプログラムにインポートするにはどうすればよいですか?Python天気API

答えて

39

Googleは天気APIをシャットダウンしているので、私はOpenWeatherMapをチェックアウトすることをお勧め:

OpenWeatherMapサービスは、無料の気象データやウェブやスマートフォン アプリケーションのような任意の製図サービスに適し予報API を提供します。イデオロギーはOpenStreetMapとWikipediaに触発されています。 は誰もが情報を無料で利用できるようにしています。 OpenWeatherMap は、現在の天気の地図、 週間の予報、降水量、風、雲、気象局 などからのデータなど、幅広い天候データを提供しています。天気データは、気象衛星 の放送サービスと40 000以上の気象観測局から受信されます。

これはPythonライブラリではありませんが、JSON形式で結果を得ることができるため、使いやすいです。

ここ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}} 

そしてここではPyOWM、OpenWeatherMapウェブAPI周りのPythonラッパーを使用した例です:

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

公式APIドキュメントは、利用可能ですhereです。 here

+2

OpenWeatherMapウェブAPIリソースがエンドポイントに応じて、異なるJSONの塊を使用してフォーマットされている天気図を開くには、APIキーサインアップ取得するには

。したがって、解析はひどい...このような手間を省き、外部ライブラリを使用してホイールを再開発しないでください - 例:PyOWM https://github.com/csparpa/pyowm – csparpa

+0

@csparpaありがとう、私は答えを更新しました! –

+0

mmm興味深い。 {u'speed ':3.1、u'deg':220}からどのように速度を印刷するのですか? @paolo –