2020---1/10 实时天气较为准确
import urllib.request #提取网址数据
import gzip
import json
print('------天气查询------')
def get_weather_data() :
city_name = input('请输入要查询的城市名称:')
#网址1只需要输入城市名,网址2需要输入城市代码
#说明:URL只允许一部分ASCII字符,其他字符(如汉字)是不符合标准的,此时就要引用quote进行编码转换
url1 = 'http://wthrcdn.etouch.cn/weather_mini?city='+urllib.parse.quote(city_name)
url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
#用urllib.request模块的urlopen()获取指定网页页面,
weather_data = urllib.request.urlopen(url1) # HTTPResponse类型 <http.client.HTTPResponse object at 0x00000000048BC908>
#urlopen返回对象提供方法read():对HTTPResponse类型数据进行操作
weather_data = weather_data.read()
#解压网页数据
#weather_data的数据格式为bytes类型,需要decode()解码,转换成str类型
weather_data = gzip.decompress(weather_data).decode('utf-8')
#将json数据转换为dict数据
weather_dict = json.loads(weather_data)
return weather_dict
#print(get_weather_data()) #这里查看上个函数(return weather_dict)的返回值究竟是啥
def show_weather(weather_data):
#将形参数据值即(return weather_dict)赋值给这里的weather_dict变量
weather_dict = weather_data
if weather_dict.get('desc') == 'invilad-citykey':
print('你输入的城市名有误,或者天气中心未收录你所在城市')
elif weather_dict.get('desc') =='OK':
forecast = weather_dict.get('data').get('forecast')
print('城市:',weather_dict.get('data').get('city'))
print('温度:',weather_dict.get('data').get('wendu')+'℃ ')
print('感冒:',weather_dict.get('data').get('ganmao'))
print('风向:',forecast[0].get('fengxiang'))
print('风级:',forecast[0].get('fengli'))
print('高温:',forecast[0].get('high'))
print('低温:',forecast[0].get('low'))
print('天气:',forecast[0].get('type'))
print('日期:',forecast[0].get('date'))
print('*******************************')
four_day_forecast =input('是否要显示未来四天天气,是/否:')
if four_day_forecast == '是' or 'Y' or 'y':
for i in range(1,5):
print('日期:',forecast[i].get('date'))
print('风向:',forecast[i].get('fengxiang'))
print('风级:',forecast[i].get('fengli'))
print('高温:',forecast[i].get('high'))
print('低温:',forecast[i].get('low'))
print('天气:',forecast[i].get('type'))
print('--------------------------')
print('***********************************')
show_weather(get_weather_data())
#为了方便观察,大致拆分整理了一下: 这里首先是字典,有desc,city,forecast,yesterday,ganmao,wendu 6个键;其中forecast的值是一个列表。
{'desc': 'OK',
'data': {"city": '徐州',"forecast":
[{'date': '10日星期一', 'low': '低温 3℃', 'high': '高温 16℃', 'fengli': '<![CDATA[<3级]]>', 'fengxiang': '西南风', 'type': '晴'},
{'date': '11日星期二', 'low': '低温 6℃', 'high': '高温 12℃', 'fengli': '<![CDATA[<3级]]>', 'fengxiang': '南风', 'type': '多云'},
{'date': '12日星期三', 'low': '低温 5℃', 'high': '高温 15℃', 'fengli': '<![CDATA[<3级]]>', 'fengxiang': '南风', 'type': '晴'},
{'date': '13日星期四', 'low': '低温 12℃', 'high': '高温 14℃', 'fengli': '<![CDATA[<3级]]>', 'fengxiang': '东南风', 'type': '阴'},
{'date': '14日星期五', 'low': '低温 4℃', 'high': '高温 14℃', 'fengli': '<![CDATA[<3级]]>', 'fengxiang': '东风', 'type': '多云'}],
'yesterday': {'fl': '<![CDATA[<3级]]>', 'low': '低温 0℃', 'fx': '西南风', 'high': '高温 12℃', 'date': '9日星期日', 'type': '晴'},
"ganmao": '天凉,昼夜温差较大,较易发生感冒,请适当增减衣服,体质较弱的朋友请注意适当防护。', "wendu": '16'
},
'status': 1000}
参考资料:
天气对接url(中华万年历):https://www.cnblogs.com/yunjuanyunshu/p/9234037.html
python3(urllib模块的使用):https://www.cnblogs.com/Lands-ljk/p/5447127.html
python3(urllib.parse.quote):https://blog.youkuaiyun.com/ZTCooper/article/details/80165038