yvkepei 发表于 2020-4-3 21:52:09

[Python]天气查询程序学习

非计算机专业菜鸟小白,勿笑{:5_96:}
在论坛上下了这个代码学习,有几处不懂的地方想请教大神:
①http://wthrcdn.etouch.cn/weather_mini?citykey=101010100下载的文件算什么格式?我查了一下,文件中的内容是json格式数据
②urllib.request.urlopen(url1).read()、 gzip.decompress(weather_data).decode('utf-8')这种函数是否有比较浅显直白的说明资料(不想看官方原文)
③下面这几句代码说的解压网页数据,是否解压第①点下载的文件?还是指其他?为什么要接解压?第①点下载的文件不像我们平时看到的zip格式文件。
weather_data = gzip.decompress(weather_data).decode('utf-8')
    print(weather_data)
    #解压网页数据

import urllib.request
import gzip
import json
print('------天气查询------')
def get_weather_data() :
    city_name = input('请输入要查询的城市名称:')
    url1 = 'http://wthrcdn.etouch.cn/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()
    print(weather_data)
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    print(weather_data)
    #解压网页数据
    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.get('fengxiang'))
      print('风级:',forecast.get('fengli'))
      print('高温:',forecast.get('high'))
      print('低温:',forecast.get('low'))
      print('天气:',forecast.get('type'))
      print('日期:',forecast.get('date'))
      print('*******************************')
      four_day_forecast =input('是否要显示未来四天天气,是/否:')
      if four_day_forecast == '是' or 'Y' or 'y':
            for i in range(1,5):
                print('日期:',forecast.get('date'))
                print('风向:',forecast.get('fengxiang'))
                print('风级:',forecast.get('fengli'))
                print('高温:',forecast.get('high'))
                print('低温:',forecast.get('low'))
                print('天气:',forecast.get('type'))
                print('--------------------------')
    print('***********************************')

show_weather(get_weather_data())

溪水叮咚 发表于 2020-4-4 09:25:53

你是不是从我那里复制过来的?
{:10_256:}

溪水叮咚 发表于 2020-4-4 09:36:51

1,下载下来的一个文件,就是一个以城市代码引出的一部分相关内容(5天的天气——一个大型的字典)
2,urllib.request.urlopen(url1).read() 是打开并读取url1,gzip.decompress(weather_data).decode('utf-8') 是用UTF-8的编码格式对weather_data进行解析
3,有了前边两项,你自己在想一想第三项,不一会就知道了

_2_ 发表于 2020-4-4 09:37:24

溪水叮咚 发表于 2020-4-4 09:25
你是不是从我那里复制过来的?

……

溪水叮咚 发表于 2020-4-4 09:42:54

_2_ 发表于 2020-4-4 09:37
……

……

yvkepei 发表于 2020-4-6 20:54:47

溪水叮咚 发表于 2020-4-4 09:25
你是不是从我那里复制过来的?

应该是{:5_109:}

溪水叮咚 发表于 2020-4-6 21:22:53

yvkepei 发表于 2020-4-6 20:54
应该是

呵呵呵,给一个最佳答案呗
页: [1]
查看完整版本: [Python]天气查询程序学习