+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-03(62)

2020-04(60)

2020-05(24)

2020-06(39)

2020-07(23)

[Python & 高德地图] API调用学习历程(四)

发布于2020-06-25 19:48     阅读(1208)     评论(0)     点赞(2)     收藏(2)


0

1

2

3

4

5

6

7

[四]高德天气API调用

  • 通过调用天气,应用在QQ机器人上,实现查天气功能
    在这里插入图片描述
  • Python代码示例:
async def get_weather_of_city(city: str) -> str:                                                                                                                        #
    #当前                                                                                                                                                               #
    parameters_now = {'key':key_Amap,                                                                                                                                   #
                  'city':city,                                                                                                                                          #
                  'extensions':'base',                                                                                                                                  #
                  'output':'json'                                                                                                                                       #
                  }                                                                                                                                                     #
    r_now = requests.get("https://restapi.amap.com/v3/weather/weatherInfo?parameters",params = parameters_now)                                                          #
    try:                                                                                                                                                                #
        weater = r_now.json()['lives'][0]['weather']                                                                                                                    #
        temp = r_now.json()['lives'][0]['temperature']                                                                                                                  #
        winddirection = r_now.json()['lives'][0]['winddirection']                                                                                                       #
        windpow = r_now.json()['lives'][0]['windpower']                                                                                                                 #
        humidity = r_now.json()['lives'][0]['humidity']                                                                                                                 #
        result_now = '当前' + city + '的天气为:' + weater + ',' + temp + '℃,空气湿度:' + humidity + ',刮' + winddirection + '风,风力' + windpow + '级!' + '\n'  #
        #预报                                                                                                                                                           #
        parameters_next = {'key':key_Amap,                                                                                                                              #
                      'city':city,                                                                                                                                      #
                      'extensions':'all',                                                                                                                               #
                      'output':'json'                                                                                                                                   #
                      }                                                                                                                                                 #
        r_next = requests.get("https://restapi.amap.com/v3/weather/weatherInfo?parameters",params = parameters_next)                                                    #
        #当日                                                                                                                                                           #
        data0_data = r_next.json()['forecasts'][0]['casts'][0]['date']                                                                                                  #
        data0_week = r_next.json()['forecasts'][0]['casts'][0]['week']                                                                                                  #
        data0_weather = r_next.json()['forecasts'][0]['casts'][0]['dayweather']                                                                                         #
        data0_tempmin = r_next.json()['forecasts'][0]['casts'][0]['nighttemp']                                                                                          #
        data0_tempmax = r_next.json()['forecasts'][0]['casts'][0]['daytemp']                                                                                            #
        data0_dic =r_next.json()['forecasts'][0]['casts'][0]['daywind']                                                                                                 #
        data0_pow = r_next.json()['forecasts'][0]['casts'][0]['daypower']                                                                                               #
        result0 = data0_data + '周' + data0_week + ':' + data0_weather + ',' + data0_tempmin + '℃~' + \
                  data0_tempmax + '℃,' + data0_dic + '风' + data0_pow + '级' + '\n'                                                                                   
        #次日                                                                                                                                                           #
        data1_data = r_next.json()['forecasts'][0]['casts'][1]['date']                                                                                                  #
        data1_week = r_next.json()['forecasts'][0]['casts'][1]['week']                                                                                                  #
        data1_weather = r_next.json()['forecasts'][0]['casts'][1]['dayweather']                                                                                         #
        data1_tempmin = r_next.json()['forecasts'][0]['casts'][1]['nighttemp']                                                                                          #
        data1_tempmax = r_next.json()['forecasts'][0]['casts'][1]['daytemp']                                                                                            #
        data1_dic =r_next.json()['forecasts'][0]['casts'][1]['daywind']                                                                                                 #
        data1_pow = r_next.json()['forecasts'][0]['casts'][1]['daypower']                                                                                               #
        result1 = data1_data + '周' + data1_week + ':' + data1_weather + ',' + data1_tempmin + '℃~' + \
                  data1_tempmax + '℃,' + data1_dic + '风' + data1_pow + '级' + '\n'                                                                                   
        #后日                                                                                                                                                           #
        data2_data = r_next.json()['forecasts'][0]['casts'][2]['date']                                                                                                  #
        data2_week = r_next.json()['forecasts'][0]['casts'][2]['week']                                                                                                  #
        data2_weather = r_next.json()['forecasts'][0]['casts'][2]['dayweather']                                                                                         #
        data2_tempmin = r_next.json()['forecasts'][0]['casts'][2]['nighttemp']                                                                                          #
        data2_tempmax = r_next.json()['forecasts'][0]['casts'][2]['daytemp']                                                                                            #
        data2_dic =r_next.json()['forecasts'][0]['casts'][2]['daywind']                                                                                                 #
        data2_pow = r_next.json()['forecasts'][0]['casts'][2]['daypower']                                                                                               #
        result2 = data2_data + '周' + data2_week + ':' + data2_weather + ',' + data2_tempmin + '℃~' + \
                  data2_tempmax + '℃,' + data2_dic + '风' + data2_pow + '级' + '\n'                                                                                   
        #大后日                                                                                                                                                         #
        data3_data = r_next.json()['forecasts'][0]['casts'][3]['date']                                                                                                  #
        data3_week = r_next.json()['forecasts'][0]['casts'][3]['week']                                                                                                  #
        data3_weather = r_next.json()['forecasts'][0]['casts'][3]['dayweather']                                                                                         #
        data3_tempmin = r_next.json()['forecasts'][0]['casts'][3]['nighttemp']                                                                                          # 
        data3_tempmax = r_next.json()['forecasts'][0]['casts'][3]['daytemp']                                                                                            #
        data3_dic =r_next.json()['forecasts'][0]['casts'][3]['daywind']                                                                                                 #
        data3_pow = r_next.json()['forecasts'][0]['casts'][3]['daypower']                                                                                               #
        result3 = data3_data + '周' + data3_week + ':' + data3_weather + ',' + data3_tempmin + '℃~' + \
                  data3_tempmax + '℃,' + data3_dic + '风' + data3_pow + '级' + '\n'                                                                                   
        result_all = result_now + result0 + result1 + result2 + result3                                                                                                 #
    except CQHttpError:                                                                                                                                                 # 
        result_all = '我似乎有点问题...换个说法试试吧~'                                                                                                                 #
    return result_all                                                                                                                                                   #

这段代码是应用在QQ机器人里面的,读者若想单独使用此功能,稍作修改即可!

原文链接:https://blog.csdn.net/qq_34916678/article/details/106950953

0

1

2

3

4

5



所属网站分类: 技术文章 > 博客

作者:9384vfnv

链接: https://www.pythonheidong.com/blog/article/430392/206955404b13d39c3244/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

2 0
收藏该文
已收藏

评论内容:(最多支持255个字符)