LoginSignup
12
18

More than 1 year has passed since last update.

【Python】気象庁API から天気予報を取得してみた

Last updated at Posted at 2021-10-27

初めに

ダッシュボード作成のために、天気予報をAPIにて取得する。
1年くらい前の気象庁のHPが大幅リニューアルし、非公式ながらAPIが発見された。そのAPIを用い「天気予報」を取得したいと思う。気象庁 にて使用されているAPIを使用。詳しいことは気象庁ホームページ防災気象情報のURL構造を参考に作成した。
⇒Pythonを用い、raspberrypi等で表示させる用途を想定

※非公式のため、無保証。アップデート等により使用不可になる可能性もあり。
政府標準利用規約(PDF)に準拠して利用すること
PHPバージョン

環境

  • Windows10
  • Python 3.8.12

データの詳細

エリアのコード(pathCode) 東京は130000
http://www.jma.go.jp/bosai/common/const/area.json

コンテンツの種別は
http://www.jma.go.jp/bosai/common/const/contents.json

テロップ番号
https://plaza.rakuten.co.jp/rabbit77/3000/

コード

jma_weather_api.py
# -*- coding:utf-8 -*-
import requests
import json

# 気象庁データの取得
jma_url = "https://www.jma.go.jp/bosai/forecast/data/forecast/{エリアコード}.json"
jma_json = requests.get(jma_url).json()

# 取得したいデータを選ぶ
jma_date = jma_json[0]["timeSeries"][0]["timeDefines"][0]
jma_weather = jma_json[0]["timeSeries"][0]["areas"][0]["weathers"][0]
jma_rainfall = jma_json["Feature"][0]["Property"]["WeatherList"]["Weather"][0]["Rainfall"]
# 全角スペースの削除
jma_weather = jma_weather.replace(' ', '')

print(jma_date)
print(jma_weather)
print(jma_rainfall)

終わりに

非公式とは言え、気象庁の情報を簡単に使用できるのは便利ですね。
HPに気象庁の天気予報の掲載ができます。

参考サイト

12
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
18