xd123456 发表于 2018-5-4 09:52:53

全国天气查询

import urllib.request
import gzip
import json
print('------021王掌柜 天气查询------')
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()
    #读取网页数据
    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.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 four_day_forecast =='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())
b='yes'
b=input('查询继续,输入NO 程序退款')
while b!='no'or b!='NO':
   show_weather(get_weather_data())

野花菜子 发表于 2018-5-4 10:17:18

给楼主点个赞👍

lsp84ch83 发表于 2018-7-9 11:53:24

你这个运行后,会一直循环,无法退出,会一直循环 输入查询城市->返回结果->是否显现最近4天,不管输入什么 又是 输入查询城市

风味发酵乳 发表于 2018-7-9 17:47:47

很厉害啊,虽然根本看不懂{:10_306:}

我想学拍肾 发表于 2018-8-16 17:11:00

lsp84ch83 发表于 2018-7-9 11:53
你这个运行后,会一直循环,无法退出,会一直循环 输入查询城市->返回结果->是否显现最近4天,不管输入什么 ...

请问有什么办法解决吗,我想知道

j8win 发表于 2020-4-14 13:00:03

赞!~
页: [1]
查看完整版本: 全国天气查询