鱼C论坛

 找回密码
 立即注册
楼主: python_小白

[作品展示] 新人天气查询程序,绝对可用

    [复制链接]
发表于 2016-11-9 08:45:39 | 显示全部楼层
想起了以前老板让我调用 株洲的天气情况,头疼啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-9 17:31:35 | 显示全部楼层
11
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-9 21:12:40 | 显示全部楼层
看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-9 21:51:07 | 显示全部楼层
本帖最后由 jackie-L 于 2016-11-9 22:12 编辑

我是新手,我觉得网络数据解压这部分很值得学习,但你的代码我觉得不够舒服,还有几个地方代码有问题,我稍微改了下,见笑了

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

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


  44. city_name = input('请输入要查询的城市名称(输入quit则退出程序):')

  45. while city_name != 'quit':   
  46.     weather_dict = get_weather_data(city_name)
  47.     show_weather(weather_dict)
  48.     city_name = input('请输入要查询的城市名称(输入quit则退出程序):')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-11 09:52:55 | 显示全部楼层
新手下载 学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-15 12:45:35 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-16 20:02:36 | 显示全部楼层
下载 做任务
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-16 21:56:07 | 显示全部楼层
新手来学习下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-16 22:58:10 | 显示全部楼层
看看,学习学习。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-21 14:21:32 | 显示全部楼层
学习学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-22 17:37:45 | 显示全部楼层
OK
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-22 23:55:28 | 显示全部楼层
牛逼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-23 09:22:40 | 显示全部楼层
学习ing
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-23 09:27:37 | 显示全部楼层
跟各位大神好好学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-23 10:20:12 | 显示全部楼层
天气预报
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-23 10:41:08 | 显示全部楼层
很好
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-25 08:49:10 | 显示全部楼层
嗯呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-25 15:55:27 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-11-26 21:31:12 | 显示全部楼层
过来看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-26 22:50:28 | 显示全部楼层
不错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 23:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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