Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 天気データへのアクセス | 公開データAPIの活用
PythonでのAPI操作

book天気データへのアクセス

メニューを表示するにはスワイプしてください

Open-Meteo APIは、無料で使いやすいサービスであり、登録やAPIキーの取得なしに天気予報や現在の気象データへアクセス可能。地理座標(latitudeおよびlongitude)を指定するだけで天気データを取得できるため、迅速な実験やPythonで実際の気象データを扱う学習に最適。

12345678910111213141516171819
import requests # Set the coordinates for New York City latitude = 40.7128 longitude = -74.0060 # Prepare the API endpoint and parameters url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } # Make the GET request to the Open-Meteo API response = requests.get(url, params=params) # Print the JSON response print(response.json())
copy

上記のコードでは、Open-Meteo APIから気象データを取得するために3つの主要なパラメータを使用:

  • Latitude:位置の南北方向を指定;
  • Longitude:位置の東西方向を指定;
  • Current_weather:指定した座標の現在の気象データを取得するためにTrueに設定。

これらのパラメータにより、地球上の任意の地点を特定し、その場所の最新の気象情報を取得可能。

123456789101112131415161718192021
import requests latitude = 40.7128 longitude = -74.0060 url = "https://api.open-meteo.com/v1/forecast" params = { "latitude": latitude, "longitude": longitude, "current_weather": True } response = requests.get(url, params=params) data = response.json() # Extract temperature and wind speed from the current weather data temperature = data["current_weather"]["temperature"] wind_speed = data["current_weather"]["windspeed"] print("Temperature:", temperature, "°C") print("Wind speed:", wind_speed, "km/h")
copy

APIレスポンスから天気データのフィールドを解釈する際は、単位に注意することが重要です。たとえば、気温は摂氏(°C)で提供され、風速は時速キロメートル(km/h)で示されます。これらの単位を理解することで、天気データを正確に表示・分析できます。その他のフィールド、たとえば風向は通常度数で表され、は北、90°は東、180°は南、270°は西を意味します。

1. Open-Meteo APIで場所を指定するために必要なパラメータはどれですか?

2. Pythonでresponse.json()が返すデータ構造の型は何ですか?

question mark

Open-Meteo APIで場所を指定するために必要なパラメータはどれですか?

正しい答えを選んでください

question mark

Pythonでresponse.json()が返すデータ構造の型は何ですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 2.  1

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 2.  1
some-alt