猫史档案馆


【Python作品分享】天气查询【教程贴】

用户:Jxpi409Jxpi409查看:2 回复:4 评论:2 创建时间:2020-08-04T16:02:34


【作品展示】

center_image

 

【作品介绍】

嗯,天气查询,是我做了很久才做出来的,做的不好,请谅解,也希望能帮到大家

 

【作品源代码】

import urllib.request
import gzip
import json
print('------天气查询------')
def get_weather_data() :
    city_name = input('请输入要查询的城市名称:')
    url喵weather_mini?city='+urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    #网址1只需要输入城市名,网址2需要输入城市代码
    #print(url1)
    weather_data = urllib.request.urlopen(url1).read()
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    #解压网页数据
    weather_dict = json.loads(weather_data)
    #将json数据转换为dict数据
    return weather_dict

def show_weather(weather_data):
    weather_dict = weather_data 
    #将json数据转换为dict数据
    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'or'YES'or'yes'or'Yes'or'好':
            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())

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
Jxpi409Jxpi409

Tips:不用安装库

点赞0


评论


指令者指令者

这个链接???

喵喵喵???

点赞0


评论


Jxpi409Jxpi409

  url1 = 'h t t p ://wth rc dn.e tou ch.cn/ we ather_min i?city= '+urlli .parse.q uote(city_name) url2 = ' http://wt hrcdn. tou c h.cn /we  ath er _mini?ci tykey=101010 100'    

点赞0


评论


Jxpi409Jxpi409

123421343214

点赞0


评论