最近寒くなってきたし、ニュースをみると新型コロナウィルスも気になって、今日はお家でゴロゴロしながら天気予報の情報を取得する API(OpenWeatherMap) を使って遊んでみました。
OpenWeatherMap
OpenWeatherMap は無料で気象情報を取得できる API です。
OpenWeatherMapとは
2014年に設立したOpenweather Ltd.が運営しています。 気象・予報データと衛星画像の配信をしており、機械学習のAPIも公開予定だそうです。
登録されている観測点
観測地点は以下のページから確認できます。
openweathermap.org
APIでできること
内容 | API |
---|---|
現在の気象情報を取得 | Current weather data - OpenWeatherMap |
3時間毎5日分の予報をまとめて取得 | 5 day weather forecast - OpenWeatherMap |
降水量、雲、気圧、気温など、さまざまな種類の天気図を取得 | Weather maps 1.0 - OpenWeatherMap |
過去、現在、予報の紫外線指数を取得 | UV Index - OpenWeatherMap |
気象条件にトリガーを設け、アラートか確認できる | https://openweathermap.org/triggers |
使い方
アカウント登録とAPIキーの取得
「Create New Account」ページからアカウントを登録します。
home.openweathermap.org
API Keyを取得するにはアカウントを作る必要があります。
We will use information you provided for management and administration purposes, and for keeping you informed by mail, telephone, email and SMS of other products and services from us and our partners. You can proactively manage your preferences or opt-out of communications with us at any time using Privacy Centre. You have the right to access your data held by us or to request your data to be deleted. For full details please see the OpenWeather Privacy Policy. □ I am 16 years old and over □ I agree with Privacy Policy, Terms and conditions of sale and Websites terms and conditions of use I consent to receive communications from OpenWeather Group of Companies and their partners: □ System news (API usage alert, system update, temporary system shutdown, etc) □ Product news (change to price, new product features, etc) □ Corporate news (our life, the launch of a new service, etc)
16才以上かどうか プライバシーポリシーに同意できるか システムニュース(API使用状況アラート、システム更新、一時的なシステムシャットダウンなど)がいるかどうか 製品ニュース(価格変更、新製品機能など)がいるかどうか 企業ニュース(私たちの生活、新サービスの開始など)がいるかどうか ロボットじゃなかったらチェック
用途について
登録のお知らせ
API Keyの取得
情報取得
スクリプト
import requests import json city = "tokyo" API_KEY = "{{API Key}}" # {{API Key}} api = "http://api.openweathermap.org/data/2.5/weather?units=metric&q={city}&APPID={key}" url = api.format(city = city, key = API_KEY) response = requests.get(url) data = response.json() jsonText = json.dumps(data, indent=2) print(jsonText)
取得できた情報
{ "coord": { "lon": 139.69, "lat": 35.69 }, "weather": [ { "id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04n" } ], "base": "stations", "main": { "temp": 8.21, "feels_like": 4.11, "temp_min": 7.78, "temp_max": 8.89, "pressure": 1024, "humidity": 87 }, "visibility": 10000, "wind": { "speed": 4.6, "deg": 20 }, "clouds": { "all": 75 }, "dt": 1607160881, "sys": { "type": 1, "id": 8074, "country": "JP", "sunrise": 1607117772, "sunset": 1607153270 }, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200 }