天気取得Ver1.0
#!/usr/bin/rnv python3 | |
# quickWeather 東京と那須と逗子の天気を表示 | |
import json, requests, sys | |
# city番号を定義 | |
tokyo = '130010' | |
nasu = '090020' # 大田原 | |
zushi = '140010' # 横浜 | |
# https://weather.tsukumijima.net/api/forecast のAPIからJSONデータをダウンロードする | |
url = 'https://weather.tsukumijima.net/api/forecast/city/{}'.format(tokyo) | |
response_tokyo = requests.get(url) | |
response_tokyo.raise_for_status | |
url = 'https://weather.tsukumijima.net/api/forecast/city/{}'.format(nasu) | |
response_nasu = requests.get(url) | |
response_nasu.raise_for_status | |
url = 'https://weather.tsukumijima.net/api/forecast/city/{}'.format(zushi) | |
response_zushi = requests.get(url) | |
response_zushi.raise_for_status | |
# JSONデータからPython変数に読み込む | |
# 東京の天気 | |
weather_data_tokyo = json.loads(response_tokyo.text) | |
print(weather_data_tokyo['title']) | |
w = weather_data_tokyo['forecasts'] | |
# 今日(2021-02-14):晴れ みたいな形式 | |
for i in range(3): | |
print(f'{w[i]["dateLabel"]}({w[i]["date"]}):{w[i]["telop"]}') | |
print('-----') | |
# 逗子の天気 | |
weather_data_zushi = json.loads(response_zushi.text) | |
print(weather_data_zushi['title']) | |
w = weather_data_zushi['forecasts'] | |
# 今日(2021-02-14):晴れ みたいな形式 | |
for i in range(3): | |
print(f'{w[i]["dateLabel"]}({w[i]["date"]}):{w[i]["telop"]}') | |
print('-----') | |
# 那須の天気 | |
weather_data_nasu = json.loads(response_nasu.text) | |
print(weather_data_nasu['title']) | |
w = weather_data_nasu['forecasts'] | |
# 今日(2021-02-14):晴れ みたいな形式 | |
for i in range(3): | |
print(f'{w[i]["dateLabel"]}({w[i]["date"]}):{w[i]["telop"]}') | |
print('-----') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
課題:繰り返しが多いのでforなどでまとめて短くする