■ 気象情報API
【1】お天気Webサービス From livedoor << サービス終了 【2】OpenWeatherMap 【3】気象情報API From yahoo 【4】天気予報API From 日本気象協会
【1】お天気Webサービス From livedoor(サービス終了)
* 無料。ログインIDも不要。
【2】OpenWeatherMap
* 無料だが、API Keyが必要。
公式サイト
https://openweathermap.org/
参考文献
https://qiita.com/nownabe/items/aeac1ce0977be963a740
【3】気象情報API From yahoo
* 無料だが、ログインIDが必要。
公式サイト
https://developer.yahoo.co.jp/webapi/map/openlocalplatform/v1/weather.html
【4】天気予報API From 日本気象協会
* 有料
公式サイト
https://www.jwa.or.jp/service-business/service/29.html
■ 環境設定
* REST APIにリクエストを送るために HTTP ライブラリ「requests」をインストールする
手順
# pipのインストール curl -kL https://bootstrap.pypa.io/get-pip.py | python # requestsのインストール sudo pip install requests
■ サンプル
* 「【1】お天気Webサービス From livedoor」を使用した場合
hello.py
# -*- coding: utf-8 -*- import requests url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010' api_data = requests.get(url).json() print(api_data['title']) for weather in api_data['forecasts']: weather_date = weather['dateLabel'] weather_forecasts = weather['telop'] print(weather_date + ':' + weather_forecasts)
実行および出力結果
python hello.py 東京都 東京 の天気 今日:曇り 明日:曇り
参考文献
http://tarao-mendo.blogspot.com/2018/03/python-requests-weather-api.html