转
import urllib.request
import json
import gzip
import re
def show_weather():
city_name = input("你想查询的城市?\n")
#访问的url,其中urllib.parse.quote是将城市名转换为url的组件
url = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
#发出请求并读取到weather_data
weather_data = urllib.request.urlopen(url).read()
#以utf-8的编码方式解压数据
weather_data = gzip.decompress(weather_data).decode('utf-8')
#将json数据转化为dict数据
weather_dict = json.loads(weather_data)
#print('weather_dict=', (weather_dict))
#print('weather = {}'.format(weather_dict))
if weather_dict.get('desc') == 'invilad-citykey':
print("输入的城市名有误")
elif weather_dict.get('desc') =='OK':
forecast = weather_dict.get('data').get('forecast')
#print('风力:', (((forecast[0].get('fengli')).split('['))[2].split(']'))[0])
#print('风力 = ', re.split('[[<]',forecast[0].get('fengli')))
startoday = '城市:'+weather_dict.get('data').get('city') +'\n' \
+ '日期:'+forecast[0].get('date') + '\n'\
+ '温度&#x