zoukankan      html  css  js  c++  java
  • 天气查询(源代码)

    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()
    #读取网页数据
    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[0].get('fengxiang'))
    print('风级:',forecast[0].get('fengli'))
    print('高温:',forecast[0].get('high'))
    print('低温:',forecast[0].get('low'))
    print('天气:',forecast[0].get('type'))
    print('日期:',forecast[0].get('date'))
    print('*******************************')
    four_day_forecast =input('是否要显示未来四天天气,是/否:')
    if four_day_forecast == '是' or 'Y' or 'y':
    for i in range(1,5):
    print('日期:',forecast[i].get('date'))
    print('风向:',forecast[i].get('fengxiang'))
    print('风级:',forecast[i].get('fengli'))
    print('高温:',forecast[i].get('high'))
    print('低温:',forecast[i].get('low'))
    print('天气:',forecast[i].get('type'))
    print('--------------------------')
    print('***********************************')
    input("Prease <enter>")
    show_weather(get_weather_data())

    查询结果如图:

  • 相关阅读:
    element ui 时间控件 多个日期
    java 获取两个日期之间的所有日期(年月日)
    java 正则表达式(内附例子)
    Notepad++怎么使用正则替换
    基于 vue+element ui 的cdn网站(多页面,都是各种demo)
    使用github搭建个人html网站
    PL/SQL Developer 如何记住密码
    PL/SQL Developer图形化窗口创建数据库(表空间和用户)以及相关查询sql
    安装pl/sql developer(内附下载地址)
    vue中操作cookie的插件
  • 原文地址:https://www.cnblogs.com/nnty/p/9897093.html
Copyright © 2011-2022 走看看