#! python3
# -*- coding:utf-8 -*-
# 使用api獲取json格式的天氣數據
import json, requests, sys
if len(sys.argv) != 2:
print('Usage CN_Weather.py location')
sys.exit()
location = sys.argv[1]
url = 'https://www.sojson.com/open/api/weather/json.shtml?city=%s' % location
response = requests.get(url)
response.raise_for_status()
weather = json.loads(response.text)
try:
if weather['status'] == 200:
w = weather['data']
print('今天%s的天氣爲:' % location)
print('溫度: ' + w['forecast'][0]['low'] + '~' + w['forecast'][0]['high'] +
'---' + '天氣: ' + w['forecast'][0]['type'])
print(w['forecast'][0]['notice'])
print()
print('明天的天氣:')
print('溫度: ' + w['forecast'][1]['low'] + '~' + w['forecast'][1]['high'] +
'---' + '天氣: ' + w['forecast'][1]['type'])
print(w['forecast'][1]['notice'])
print()
print('後天的天氣:')
print('溫度: ' + w['forecast'][2]['low'] + '~' + w['forecast'][2]['high'] +
'---' + '天氣: ' + w['forecast'][2]['type'])
print(w['forecast'][2]['notice'])
except:
print('請輸入正確具體的地名,精確到市一級')