LoginSignup
12
8

More than 3 years have passed since last update.

OpenWeatherMapで大阪の天気を取得した際の備忘録(python)

Posted at

目的

OpenWeatherMapで大阪の天気を取得した際の備忘録です(python)

準備

OpenWeatherMapに登録してAPI Keyを取得する

OpenWeatherMap

コード

公式サイトのAPIを参考にします

Weather API

Current weather data

sample.py
!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import requests

city = "Osaka"
key = 'xxxxxxxxxxxxxxxxxxxxxx'  # your API Key
url = 'http://api.openweathermap.org/data/2.5/weather?units=metric&q=' + city + '&APPID=' + key

print(url)
response = requests.get(url)

print("---")
data = response.json()
jsonText = json.dumps(data, indent=4)
print(jsonText)
print("---")
data = json.loads(response.text)
print(city)
print("weather:", data["weather"][0]["main"])
print("temp:", data["main"]["temp"])
print("pressure:", data["main"]["pressure"])
print("humidity:", data["main"]["humidity"])
print("temp_min:", data["main"]["temp_min"])
print("temp_max:", data["main"]["temp_max"])

実行結果

$ python sample.py
---
{
    "coord": {
        "lon": 135.5,
        "lat": 34.7
    },
    "weather": [
        {
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03n"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 29,
        "pressure": 995,
        "humidity": 74,
        "temp_min": 29,
        "temp_max": 29
    },
    "visibility": 10000,
    "wind": {
        "speed": 8.2,
        "deg": 70
    },
    "clouds": {
        "all": 40
    },
    "dt": 1565792792,
    "sys": {
        "type": 1,
        "id": 7962,
        "message": 0.007,
        "country": "JP",
        "sunrise": 1565727433,
        "sunset": 1565776118
    },
    "timezone": 32400,
    "id": 1853909,
    "name": "Osaka",
    "cod": 200
}
---
Osaka
weather: Clouds
temp: 29
pressure: 995
humidity: 74
temp_min: 29
temp_max: 29

Call 5 day / 3 hour forecast data

sample.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import requests

city = "Osaka"
key = 'xxxxxxxxxxxxxxxxxxxx' # your API Key
url = 'http://api.openweathermap.org/data/2.5/forecast?units=metric&q=' + city + '&APPID=' + key

print(url)
response = requests.get(url)

print("---")
data = response.json()
data = json.loads(response.text)

print(city)

for i in range( len(data["list"]) ):
    print(data["list"][i]["dt_txt"])
    print("   temp", data["list"][i]["weather"][0]["main"])
    print("   temp", data["list"][i]["main"]["temp"])
    print("   pressure:", data["list"][i]["main"]["pressure"])
    print("   humidity:", data["list"][i]["main"]["humidity"])
    print("   temp_min:", data["list"][i]["main"]["temp_min"])
    print("   temp_max:", data["list"][i]["main"]["temp_max"])

実行結果

$ python sample.py
---
Osaka
2019-08-14 15:00:00
   temp Clouds
   temp 28.31
   pressure: 996.76
   humidity: 80
   temp_min: 26.35
   temp_max: 28.31
2019-08-14 18:00:00
   temp Clouds
   temp 28.13
   pressure: 995.19
   humidity: 77
   temp_min: 26.66
   temp_max: 28.13
2019-08-14 21:00:00
   temp Clouds
   temp 28.44
   pressure: 993.6
   humidity: 72
   temp_min: 27.46
   temp_max: 28.44
...
2019-08-19 06:00:00
   temp Rain
   temp 32.74
   pressure: 1007.19
   humidity: 52
   temp_min: 32.74
   temp_max: 32.74
2019-08-19 09:00:00
   temp Rain
   temp 28.07
   pressure: 1007.76
   humidity: 75
   temp_min: 28.07
   temp_max: 28.07
2019-08-19 12:00:00
   temp Rain
   temp 26.07
   pressure: 1008.73
   humidity: 81
   temp_min: 26.07
   temp_max: 26.07

このままでは見づらいので、グラフ化したい。
いいグラフ化ツールはないだろうか。

参考

無料天気予報APIのOpenWeatherMapを使ってみる
OpenWeatherMap
pythonを使ってOpenWeatherMapから天気情報を取得
OpenWeatherMap APIをPythonで叩いてパースするメモ
【WebAPI】OpenWeatherMapで3時間ごとの天気を取りたい【json】

12
8
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
8