Webサイトから気象画像を取得するだけなら簡単

雲の様子を取得するだけなら、SDRを購入しアンテナを作るといった手間は要りません。Webサイトから最新の衛星画像をダウンロードするだけです。

プログラムはこんな感じ。

import requests
from bs4 import BeautifulSoup
import time
import os

# JMAのひまわり画像ページURL(例:最新の画像を取得)
JMA_URL = "https://tenki.jp/satellite/japan-near/"

# 保存先ディレクトリ
SAVE_DIR = "./himawari_images"
os.makedirs(SAVE_DIR, exist_ok=True)

def get_latest_image_url():
    try:
        # JMAのページを取得
        response = requests.get(JMA_URL, verify=False)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, "html.parser")

        # 画像URLを検索(例:HTML内のimgタグのsrc属性)
        # 実際のHTML構造に合わせて調整が必要です
        image_url = None
        elems = soup.find_all("img")
        for img in elems:
            if img.get("id") == "satellite-image":
                image_url = img["src"]
                break

        if image_url:
            # 相対パスを絶対URLに変換
            if image_url.startswith("//"):
                image_url = "https:" + image_url
            return image_url
        else:
            print("画像URLを取得できませんでした。")
            return None
    except Exception as e:
        print(f"エラー: {e}")
        return None

def download_image(url, filename):
    try:
        response = requests.get(url)
        response.raise_for_status()
        with open(filename, "wb") as f:
            f.write(response.content)
        print(f"画像を保存しました: {filename}")
    except Exception as e:
        print(f"画像ダウンロードエラー: {e}")

def main():
    while True:
        image_url = get_latest_image_url()
        if image_url:
            # 現在日時をファイル名に含める
            timestamp = time.strftime("%Y%m%d_%H%M%S")
            filename = os.path.join(SAVE_DIR, f"himawari_{timestamp}.jpg")
            download_image(image_url, filename)
        else:
            print("画像取得に失敗しました。")
        # 10分ごとに実行(適宜調整)
        time.sleep(600)

if __name__ == "__main__":
    main()

cronで画像取得をスケジュール化する、取得した画像をDiscordコミュニティーサーバーに送信するなど、あと一手間かかればそれなりに使えるものになりそうですが、個人的にはあまり面白くない。

この程度のプログラムなら一から書いてもそれほど時間がかからないし、ローカルLLMにコード生成してもらって少し手直しするという方法なら数分で動くものが得られると思います。

ちなみにちょっと調べてみたところ、前の投稿の方法(SDR + Raspberry Pi NOAA)では日本の気象衛星ひまわりの画像は取れないようです。NOAAやMeteor-Mなどとは通信方式が違って、大掛かりなシステムを構築しないといけないようです。

「Webサイトから気象画像を取得するだけなら簡単」への1件のフィードバック

コメントする