鱼C论坛

 找回密码
 立即注册
查看: 739|回复: 6

[已解决][Python]天气查询程序学习

[复制链接]
发表于 2020-4-3 21:52:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
非计算机专业菜鸟小白,勿笑
在论坛上下了这个代码学习,有几处不懂的地方想请教大神:
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)
    #解压网页数据

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

  20. def show_weather(weather_data):
  21.     weather_dict = weather_data
  22.     #将json数据转换为dict数据
  23.     if weather_dict.get('desc') == 'invilad-citykey':
  24.         print('你输入的城市名有误,或者天气中心未收录你所在城市')
  25.     elif weather_dict.get('desc') =='OK':
  26.         forecast = weather_dict.get('data').get('forecast')
  27.         print('城市:',weather_dict.get('data').get('city'))
  28.         print('温度:',weather_dict.get('data').get('wendu')+'℃ ')
  29.         print('感冒:',weather_dict.get('data').get('ganmao'))
  30.         print('风向:',forecast[0].get('fengxiang'))
  31.         print('风级:',forecast[0].get('fengli'))
  32.         print('高温:',forecast[0].get('high'))
  33.         print('低温:',forecast[0].get('low'))
  34.         print('天气:',forecast[0].get('type'))
  35.         print('日期:',forecast[0].get('date'))
  36.         print('*******************************')
  37.         four_day_forecast =input('是否要显示未来四天天气,是/否:')
  38.         if four_day_forecast == '是' or 'Y' or 'y':
  39.             for i in range(1,5):
  40.                 print('日期:',forecast[i].get('date'))
  41.                 print('风向:',forecast[i].get('fengxiang'))
  42.                 print('风级:',forecast[i].get('fengli'))
  43.                 print('高温:',forecast[i].get('high'))
  44.                 print('低温:',forecast[i].get('low'))
  45.                 print('天气:',forecast[i].get('type'))
  46.                 print('--------------------------')
  47.     print('***********************************')

  48. show_weather(get_weather_data())
复制代码
最佳答案
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,有了前边两项,你自己在想一想第三项,不一会就知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-4 09:25:53 | 显示全部楼层
你是不是从我那里复制过来的?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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,有了前边两项,你自己在想一想第三项,不一会就知道了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 09:37:24 From FishC Mobile | 显示全部楼层
溪水叮咚 发表于 2020-4-4 09:25
你是不是从我那里复制过来的?

……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-4 09:42:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-6 20:54:47 | 显示全部楼层
溪水叮咚 发表于 2020-4-4 09:25
你是不是从我那里复制过来的?

应该是
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-6 21:22:53 | 显示全部楼层

呵呵呵,给一个最佳答案呗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 09:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表