「天気予報を自動で取得して可視化したい」そんな思いから、AccuWeather APIを使って東京都世田谷区の1時間ごとの天気と気温、湿度を取得し、CSV形式で保存するPythonスクリプトを作成しました。
さらにこのスクリプトを、Ubuntuのcronで毎日午前6時と午後6時に自動実行するところまでやってみました!
AccuWeatherは、アメリカの民間気象企業が提供する天気情報サービスで、短期予報の精度が高いのが特徴です。開発者登録をすれば、以下のようなAPIを無料で使えます:
🌐 AccuWeather Developer Portal
以下の手順で、Pythonスクリプト(weather_export.py)を作成しました。
def get_location_key(api_key, city, country_code):
...
def get_hourly_forecast(api_key, location_key):
...
with open(filename, mode="w", newline='', encoding="utf-8") as file:
...
cronなどからも安全に呼び出せるよう、ファイルパスを絶対パスで指定しています。
Ubuntu 24.04上で、次のようにcronに登録しました(仮想環境のPythonを使用):
0 6 * * * /home/user/projects/weather/venv/bin/python /home/user/projects/weather/weather_export.py
0 18 * * * /home/user/projects/weather/venv/bin/python /home/user/projects/weather/weather_export.py
ログファイルも追記することで、動作確認もバッチリです。
| 時刻 | 天気 | 気温(℃) | 体感温度(℃) | 湿度(%) |
|---|---|---|---|---|
| 2025-07-27 06:00 | Intermittent clouds | 27.5 | 31.8 | 78 |
| 2025-07-27 07:00 | Partly sunny | 29.1 | 34.3 | 72 |
| 2025-07-27 08:00 | Partly sunny | 30.7 | 36.7 | 66 |
| 2025-07-27 09:00 | Partly sunny | 32.2 | 38.7 | 60 |
この仕組みをベースに、以下のような拡張も可能です:
APIの力を使えば、日々の生活がどんどん便利になりますね!
import csv
import requests
import json
from datetime import datetime
API_KEY = "あなたのAPIキー"
CITY = "あなたの都市"
COUNTRY_CODE = "JP"
def get_location_key(api_key, city, country_code):
url = f"http://dataservice.accuweather.com/locations/v1/cities/{country_code}/search"
params = {"apikey": api_key, "q": city}
response = requests.get(url, params=params)
if response.status_code != 200:
print(f"Error: {response.status_code}")
return None
data = response.json()
if not isinstance(data, list) or len(data) == 0:
print("都市が見つかりません")
return None
return data[0]["Key"]
def get_hourly_forecast(api_key, location_key):
url = f"http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/{location_key}"
params = {"apikey": api_key, "details": "true", "metric": "true"}
response = requests.get(url, params=params)
if response.status_code != 200:
print(f"Error: {response.status_code}")
return None
return response.json()
# 現在日時を取得してフォーマット
timestamp = datetime.now().strftime("%Y%m%d-%H%M")
filename = f"/home/user/projects/weather/WeatherForecast_{timestamp}.csv"
location_key = get_location_key(API_KEY, CITY, COUNTRY_CODE)
forecast = get_hourly_forecast(API_KEY, location_key)
if forecast:
with open(filename, mode="w", newline='', encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow(["DateTime", "Condition", "Temperature(°C)", "FeelsLike(°C)", "Humidity(%)"])
for hour in forecast:
writer.writerow([
hour["DateTime"],
hour["IconPhrase"],
hour["Temperature"]["Value"],
hour["RealFeelTemperature"]["Value"],
hour["RelativeHumidity"]
])
print(f"✅ CSV出力完了:{filename}")
else:
print("⚠️ 予報取得に失敗しました。")