天気を取得☔ OpenWeatherMapAPIを使った実装

Python

OpenWeatherMap API

無料の天気予報APIです。

openweathermap.org

実装

HTTPリクエストを簡単に使えるrequestsモジュールをインストールします。

pip install requests

福岡の天気を取得するプログラムを書きます。

import requests CITY_NAME = "福岡" API_KEY = "api key" # f文字列 URL = f"http://api.openweathermap.org/data/2.5/weather?q={CITY_NAME}&appid={API_KEY}" response = requests.get(URL) data = response.json() print(data)

実行

{ "coord": { "lon": 136.9231, "lat": 36.7069 }, "weather": [ { "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" } ], "base": "stations", "main": { "temp": 295.93, "feels_like": 296.3, "temp_min": 295.38, "temp_max": 297.44, "pressure": 1016, "humidity": 78 }, "visibility": 10000, "wind": { "speed": 0.45, "deg": 36, "gust": 0.89 }, "clouds": { "all": 6 }, "dt": 1630896377, "sys": { "type": 2, "id": 2041333, "country": "JP", "sunrise": 1630873637, "sunset": 1630919681 }, "timezone": 32400, "id": 1851032, "name": "Fukuoka", "cod": 200 }

参考

https://parco1021.hatenablog.com/entry/2019/10/12/223525

ふっけ / ハックツ

@fukke0906

目次