16
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【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)

レスポンスの例

東京にて取得した例
response.js
[{
	"publishingOffice": "気象庁",
	"reportDatetime": "2025-01-13T11:00:00+09:00",
	"timeSeries": [{
		"timeDefines": ["2025-01-13T11:00:00+09:00", "2025-01-14T00:00:00+09:00", "2025-01-15T00:00:00+09:00"],
		"areas": [{
			"area": {
				"name": "東京地方",
				"code": "130010"
			},
			"weatherCodes": ["100", "100", "101"],
			"weathers": ["晴れ", "晴れ 夜 くもり", "晴れ 時々 くもり"],
			"winds": ["北の風 23区西部 では 北の風 やや強く", "北の風 後 南西の風", "南西の風 やや強く 後 北西の風 やや強く"],
			"waves": ["1メートル 後 1.5メートル", "0.5メートル", "1メートル"]
		}, {
			"area": {
				"name": "伊豆諸島北部",
				"code": "130020"
			},
			"weatherCodes": ["100", "101", "201"],
			"weathers": ["晴れ", "晴れ 朝晩 くもり", "くもり 時々 晴れ"],
			"winds": ["西の風 やや強く 後 北東の風 やや強く 新島 では 西の風 強く", "北東の風 後 南西の風 強く", "西の風 強く"],
			"waves": ["2メートル 後 1.5メートル ただし 新島 では 3メートル 後 2メートル", "1.5メートル 後 2.5メートル ただし 新島 では 2メートル 後 2.5メートル うねり を伴う", "2.5メートル ただし 新島 では 2.5メートル 後 3メートル"]
		}, {
			"area": {
				"name": "伊豆諸島南部",
				"code": "130030"
			},
			"weatherCodes": ["201", "201", "202"],
			"weathers": ["くもり 時々 晴れ 所により 夜のはじめ頃 まで 雨", "くもり 時々 晴れ", "くもり 一時 雨"],
			"winds": ["北西の風 強く 後 やや強く", "北西の風 やや強く 後 西の風 やや強く", "西の風 強く"],
			"waves": ["4メートル ただし 三宅島 では 4メートル 後 2.5メートル", "3メートル うねり を伴う ただし 三宅島 では 2.5メートル 後 3メートル うねり を伴う", "3メートル"]
		}, {
			"area": {
				"name": "小笠原諸島",
				"code": "130040"
			},
			"weatherCodes": ["201", "101", "201"],
			"weathers": ["くもり 時々 晴れ", "晴れ 時々 くもり", "くもり 時々 晴れ"],
			"winds": ["北西の風 やや強く 後 北の風 やや強く", "北の風 後 北東の風", "東の風 後 南西の風"],
			"waves": ["4メートル うねり を伴う", "3メートル 後 2メートル うねり を伴う", "2メートル 後 1.5メートル うねり を伴う"]
		}]
	}, {
		"timeDefines": ["2025-01-13T12:00:00+09:00", "2025-01-13T18:00:00+09:00", "2025-01-14T00:00:00+09:00", "2025-01-14T06:00:00+09:00", "2025-01-14T12:00:00+09:00", "2025-01-14T18:00:00+09:00"],
		"areas": [{
			"area": {
				"name": "東京地方",
				"code": "130010"
			},
			"pops": ["0", "0", "0", "0", "0", "0"]
		}, {
			"area": {
				"name": "伊豆諸島北部",
				"code": "130020"
			},
			"pops": ["0", "0", "10", "10", "10", "10"]
		}, {
			"area": {
				"name": "伊豆諸島南部",
				"code": "130030"
			},
			"pops": ["20", "20", "10", "10", "10", "10"]
		}, {
			"area": {
				"name": "小笠原諸島",
				"code": "130040"
			},
			"pops": ["10", "10", "10", "10", "10", "10"]
		}]
	}, {
		"timeDefines": ["2025-01-13T09:00:00+09:00", "2025-01-13T00:00:00+09:00", "2025-01-14T00:00:00+09:00", "2025-01-14T09:00:00+09:00"],
		"areas": [{
			"area": {
				"name": "東京",
				"code": "44132"
			},
			"temps": ["13", "13", "1", "13"]
		}, {
			"area": {
				"name": "大島",
				"code": "44172"
			},
			"temps": ["14", "14", "4", "13"]
		}, {
			"area": {
				"name": "八丈島",
				"code": "44263"
			},
			"temps": ["13", "13", "9", "14"]
		}, {
			"area": {
				"name": "父島",
				"code": "44301"
			},
			"temps": ["20", "20", "15", "20"]
		}]
	}]
}, {
	"publishingOffice": "気象庁",
	"reportDatetime": "2025-01-13T11:00:00+09:00",
	"timeSeries": [{
		"timeDefines": ["2025-01-14T00:00:00+09:00", "2025-01-15T00:00:00+09:00", "2025-01-16T00:00:00+09:00", "2025-01-17T00:00:00+09:00", "2025-01-18T00:00:00+09:00", "2025-01-19T00:00:00+09:00", "2025-01-20T00:00:00+09:00"],
		"areas": [{
			"area": {
				"name": "東京地方",
				"code": "130010"
			},
			"weatherCodes": ["100", "101", "101", "101", "101", "201", "200"],
			"pops": ["", "20", "20", "20", "20", "30", "40"],
			"reliabilities": ["", "", "A", "A", "A", "A", "B"]
		}, {
			"area": {
				"name": "伊豆諸島北部",
				"code": "130020"
			},
			"weatherCodes": ["101", "201", "201", "101", "201", "200", "202"],
			"pops": ["", "30", "30", "20", "30", "40", "60"],
			"reliabilities": ["", "", "A", "A", "A", "B", "B"]
		}, {
			"area": {
				"name": "伊豆諸島南部",
				"code": "130030"
			},
			"weatherCodes": ["201", "202", "200", "200", "200", "202", "202"],
			"pops": ["", "60", "40", "40", "40", "50", "60"],
			"reliabilities": ["", "", "B", "B", "C", "C", "B"]
		}, {
			"area": {
				"name": "小笠原諸島",
				"code": "130040"
			},
			"weatherCodes": ["101", "201", "202", "202", "200", "201", "202"],
			"pops": ["", "20", "50", "50", "40", "30", "50"],
			"reliabilities": ["", "", "C", "C", "B", "A", "C"]
		}]
	}, {
		"timeDefines": ["2025-01-14T00:00:00+09:00", "2025-01-15T00:00:00+09:00", "2025-01-16T00:00:00+09:00", "2025-01-17T00:00:00+09:00", "2025-01-18T00:00:00+09:00", "2025-01-19T00:00:00+09:00", "2025-01-20T00:00:00+09:00"],
		"areas": [{
			"area": {
				"name": "東京",
				"code": "44132"
			},
			"tempsMin": ["", "4", "2", "1", "1", "1", "3"],
			"tempsMinUpper": ["", "5", "3", "3", "2", "3", "5"],
			"tempsMinLower": ["", "3", "1", "0", "-1", "0", "1"],
			"tempsMax": ["", "15", "9", "10", "9", "11", "11"],
			"tempsMaxUpper": ["", "17", "11", "12", "12", "14", "15"],
			"tempsMaxLower": ["", "13", "7", "8", "7", "8", "8"]
		}, {
			"area": {
				"name": "大島",
				"code": "44172"
			},
			"tempsMin": ["", "6", "4", "4", "4", "6", "8"],
			"tempsMinUpper": ["", "8", "6", "6", "6", "8", "10"],
			"tempsMinLower": ["", "4", "2", "2", "2", "4", "6"],
			"tempsMax": ["", "14", "12", "12", "11", "14", "13"],
			"tempsMaxUpper": ["", "16", "13", "13", "13", "16", "16"],
			"tempsMaxLower": ["", "12", "10", "10", "8", "10", "11"]
		}, {
			"area": {
				"name": "八丈島",
				"code": "44263"
			},
			"tempsMin": ["", "8", "7", "8", "8", "9", "12"],
			"tempsMinUpper": ["", "10", "9", "9", "9", "11", "13"],
			"tempsMinLower": ["", "7", "6", "7", "6", "7", "9"],
			"tempsMax": ["", "15", "11", "12", "13", "16", "16"],
			"tempsMaxUpper": ["", "16", "12", "14", "15", "17", "17"],
			"tempsMaxLower": ["", "14", "10", "11", "11", "14", "14"]
		}, {
			"area": {
				"name": "父島",
				"code": "44301"
			},
			"tempsMin": ["", "15", "17", "15", "15", "15", "17"],
			"tempsMinUpper": ["", "16", "18", "17", "16", "17", "18"],
			"tempsMinLower": ["", "14", "16", "14", "14", "14", "16"],
			"tempsMax": ["", "21", "20", "19", "19", "20", "20"],
			"tempsMaxUpper": ["", "22", "22", "20", "20", "21", "21"],
			"tempsMaxLower": ["", "20", "19", "18", "18", "19", "19"]
		}]
	}],
	"tempAverage": {
		"areas": [{
			"area": {
				"name": "東京",
				"code": "44132"
			},
			"min": "1.2",
			"max": "9.5"
		}, {
			"area": {
				"name": "大島",
				"code": "44172"
			},
			"min": "3.8",
			"max": "10.7"
		}, {
			"area": {
				"name": "八丈島",
				"code": "44263"
			},
			"min": "7.6",
			"max": "12.8"
		}, {
			"area": {
				"name": "父島",
				"code": "44301"
			},
			"min": "15.9",
			"max": "20.7"
		}]
	},
	"precipAverage": {
		"areas": [{
			"area": {
				"name": "東京",
				"code": "44132"
			},
			"min": "1.6",
			"max": "15.3"
		}, {
			"area": {
				"name": "大島",
				"code": "44172"
			},
			"min": "8.6",
			"max": "38.3"
		}, {
			"area": {
				"name": "八丈島",
				"code": "44263"
			},
			"min": "23.1",
			"max": "56.5"
		}, {
			"area": {
				"name": "父島",
				"code": "44301"
			},
			"min": "5.3",
			"max": "15.1"
		}]
	}
}]


終わりに

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

参考サイト

16
24
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
16
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?