jll421740865 发表于 2016-11-9 08:45:39

想起了以前老板让我调用 株洲的天气情况,头疼啊。

betty0311 发表于 2016-11-9 17:31:35

11

jackie-L 发表于 2016-11-9 21:12:40

看看

jackie-L 发表于 2016-11-9 21:51:07

本帖最后由 jackie-L 于 2016-11-9 22:12 编辑

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

import urllib.request
import gzip
import json
print('------天气查询------')
def get_weather_data(city_name) :   
    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()
    #读取网页数据
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    #解压网页数据
    weather_dict = json.loads(weather_data)
    #将json数据转换为dict数据
    return weather_dict

def show_weather(weather_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('是否要显示未来四天天气,y/n:')
      if four_day_forecast in ['是','Y','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('***********************************')


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

while city_name != 'quit':   
    weather_dict = get_weather_data(city_name)
    show_weather(weather_dict)
    city_name = input('请输入要查询的城市名称(输入quit则退出程序):')

fengsky 发表于 2016-11-11 09:52:55

新手下载 学习学习

王子淼 发表于 2016-11-15 12:45:35

{:5_92:}

tzhtdx 发表于 2016-11-16 20:02:36

下载 做任务

woaishenghuo 发表于 2016-11-16 21:56:07

新手来学习下

xianfa110 发表于 2016-11-16 22:58:10

看看,学习学习。

wunai_next 发表于 2016-11-21 14:21:32

学习学习

13929478844 发表于 2016-11-22 17:37:45

OK

yanliu 发表于 2016-11-22 23:55:28

牛逼

longlong2614 发表于 2016-11-23 09:22:40

学习ing

zaf 发表于 2016-11-23 09:27:37

跟各位大神好好学习

月满大唐 发表于 2016-11-23 10:20:12

天气预报

tanwen 发表于 2016-11-23 10:41:08

很好

xxb1 发表于 2016-11-25 08:49:10

嗯呢

木子飞羽 发表于 2016-11-25 15:55:27

{:5_91:}

愚鱼 发表于 2016-11-26 21:31:12

过来看看

guolzh 发表于 2016-11-26 22:50:28

不错
页: 33 34 35 36 37 38 39 40 41 42 [43] 44 45 46 47 48 49 50 51 52
查看完整版本: 新人天气查询程序,绝对可用