LoginSignup
10
11

More than 5 years have passed since last update.

Python3で天気予報表示プログラム

Posted at

1. pywaipのインストール

ネットで,「python 天気情報取得」で検索をかけ,調べた結果から,簡単そうな python-weather-api を使って,天気情報の取得と表示を行うことにしました. まず,以下のコマンドで,pywapiをインストールします.

(1) Linux Mint では

sudo apt-get install python3-pywapi

(2) Macでは

https://code.google.com/archive/p/python-weather-api/ にしたがって処理を進めます.まずpywapiをダウンロード.

gzを展開した上で,以下を実行します.(setup.pyは展開したファイル群に含まれています)

python setup.py build
python setup.py install 

2. プログラム

プログラムのコアの部分は以下の3行のみです.

import pywapi
result = pywapi.get_weather_from_weather_com('MYXX0008') # Kuala Lumpur
print(result)

情報は,weather.comからとってきています.

https://weather.com/ja-JP/weather/today/l/JAXX0085%3A1%3AJA

変数 result に色々な情報が格納されますので,必要なもののみをピックアップして表示します.

私の場合は,東京とマレーシア国クアラルンプールの現在の天気,日に出.日の入りと,5日予報を表示させています.

私が使っているプログラムは以下のとおり.

py_weather.py
# coding: utf-8
import pywapi
from datetime import datetime

def prdata(result):
    d1=[]
    d2=[]
    wt=[]
    cp=[]
    tl=[]
    th=[]
    for i in range(0,5):
        d1=d1+[result["forecasts"][i]["date"]]
        d2=d2+[result["forecasts"][i]["day_of_week"]]
        wt=wt+[result["forecasts"][i]["day"]["text"]]
        cp=cp+[result["forecasts"][i]["day"]["chance_precip"]]
        tl=tl+[result["forecasts"][i]["low"]]
        th=th+[result["forecasts"][i]["high"]]
    sr=result["forecasts"][0]["sunrise"]
    ss=result["forecasts"][0]["sunset"]
    loc=result['location']['name']
    lon=result['location']['lon']
    lat=result['location']['lat']
    udn=result['current_conditions']['last_updated']
    wtn=result['current_conditions']['text']
    ten=result['current_conditions']['temperature'] + "°C"
    print('==================================================')
    print(loc+' (lon='+lon+' lat='+lat+')')
    print('Last updated: '+udn)
    print('Weather: '+wtn+' ('+ten+')')
    print('sunrise: '+sr+' sunset: '+ss)
    print('--------------------------------------------------')
    for i in range(0,5):
        print('{0:6} {1:10} {2:3}% {3:2}/{4:2}°C {5:}'.format(d1[i],d2[i],cp[i],th[i],tl[i],wt[i]))


# Main routine
print('datetime.now: '+datetime.now().ctime())

result1 = pywapi.get_weather_from_weather_com('JAXX0085') 
prdata(result1)

result2 = pywapi.get_weather_from_weather_com('MYXX0008') 
prdata(result2)

3. 実行

ターミナルを立ち上げたらすぐに実行できるよう,Python3とプログラムのありかをフルパスで指定したスクリプトを作っています.

/Users/xx/.pyenv/shims/python3 /Users/xx/DATA/Python/py_weather.py

実行結果の例は以下のとおり.

$ bash aw.txt
datetime.now: Sun Jul 31 11:36:57 2016
==================================================
Tokyo, 13, Japan (lon=139.77 lat=35.67)
Last updated: 7/31/16 11:00 AM JST
Weather: Light Rain Shower (28°C)
sunrise: 4:48 AM sunset: 6:46 PM
--------------------------------------------------
Jul 31 Sunday     20 % 30/23°C Partly Cloudy
Aug 1  Monday     80 % 29/22°C T-Storms
Aug 2  Tuesday    80 % 28/23°C T-Storms
Aug 3  Wednesday  50 % 30/23°C Scattered T-Storms
Aug 4  Thursday   40 % 30/23°C Scattered T-Storms
==================================================
Kuala Lumpur, 14, Malaysia (lon=101.71 lat=3.16)
Last updated: 7/31/16 10:00 AM MYT
Weather: Mostly Cloudy (30°C)
sunrise: 7:12 AM sunset: 7:27 PM
--------------------------------------------------
Jul 31 Sunday     50 % 31/24°C Scattered T-Storms
Aug 1  Monday     40 % 31/24°C AM T-Storms
Aug 2  Tuesday    20 % 33/25°C Partly Cloudy
Aug 3  Wednesday  10 % 32/25°C Partly Cloudy
Aug 4  Thursday   20 % 32/25°C Partly Cloudy
$

以上

10
11
4

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
10
11