腾讯天气API解析

import requests
import json

def get_weather():
    url = "https://wis.qq.com/weather/common"
    querystring = {"source":"xw","weather_type":"forecast_1h|forecast_24h|index|alarm|limit|tips","province":"陕西","city":"西安","county":"长安"}
    headers = {}
    response = requests.request("GET", url, headers=headers, params=querystring)
    return json.loads(response.text)
def weather_time():
    print("逐小时天气预报:")
    apidata=(get_weather())["data"]["forecast_1h"]
    update_time=[]
    for i in apidata:
        update_time.append(int(i))
    update_time.sort()
    for n in update_time:
        i=(apidata[str(n)])
        print(str(i["update_time"])+":"+"温度"+str(i["degree"])+"℃、"+"天气" + i["weather"]+"、"+i["wind_power"]+"级" + i["wind_direction"]+"。" )

def weather_day():
    print("七日天气预报:")
    apidata=(get_weather())["data"]["forecast_24h"]
    update_time=[]
    for i in apidata:
        update_time.append(int(i))
    update_time.sort()
    for n in update_time:
        i=(apidata[str(n)])
        print(i["time"]+':'+'白天天气为'+i['day_weather']+","+"最高气温为"+i[ "max_degree"]+"℃,最低气温为"+i[ "min_degree"]+"℃,"+i["day_wind_power"]+"级"+i["day_wind_direction"]+';'+'晚上天气为'+i['night_weather']+","+i["day_wind_power"]+"级"+i["day_wind_direction"]+"。" )

def day():
    print("今日天气:")
    apidata=(get_weather())["data"]["forecast_24h"]
    i = (apidata[str(1)])
    print(i["time"] + ':' + '白天天气为' + i['day_weather'] + "," + "最高气温为" + i["max_degree"] + "℃,最低气温为" + i[
        "min_degree"] + "℃," + i["day_wind_power"] + "级" + i["day_wind_direction"] + ';' + '晚上天气为' + i[
              'night_weather'] + "," + i["day_wind_power"] + "级" + i["day_wind_direction"] + "。")
    tipsdata = (get_weather())["data"]["tips"]["observe"]
    print(tipsdata["0"]+"  "+tipsdata["1"])

    print('\n')
    indexdata=(get_weather())["data"]["index"]
    update_time=[]
    for i in indexdata:
        update_time.append((i))
    #print(update_time)
    for n in update_time:
        if n == "time":
            pass
        else:
            i = (indexdata[n])
            print((i["name"]+":"+i["info"]+";"+i["detail"]))



f="************************************************************************************"
print(f)
day()
print(f)
print(f)
weather_day()

print(f)
print(f)
weather_time()

input('Press Enter to exit...')

在这里插入图片描述

{
    "data": {
        "alarm": {},
        "forecast_1h": {
            "0": {
                "degree": "10",
                "update_time": "20191105090000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "1": {
                "degree": "10",
                "update_time": "20191105100000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "4"
            },
            "10": {
                "degree": "12",
                "update_time": "20191105190000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西南风",
                "wind_power": "3"
            },
            "11": {
                "degree": "12",
                "update_time": "20191105200000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西北风",
                "wind_power": "3"
            },
            "12": {
                "degree": "12",
                "update_time": "20191105210000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "13": {
                "degree": "11",
                "update_time": "20191105220000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "14": {
                "degree": "11",
                "update_time": "20191105230000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "15": {
                "degree": "11",
                "update_time": "20191106000000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "16": {
                "degree": "11",
                "update_time": "20191106010000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "17": {
                "degree": "11",
                "update_time": "20191106020000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "18": {
                "degree": "11",
                "update_time": "20191106030000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "19": {
                "degree": "11",
                "update_time": "20191106040000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "2": {
                "degree": "10",
                "update_time": "20191105110000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西北风",
                "wind_power": "3"
            },
            "20": {
                "degree": "11",
                "update_time": "20191106050000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "21": {
                "degree": "10",
                "update_time": "20191106060000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "22": {
                "degree": "9",
                "update_time": "20191106070000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "23": {
                "degree": "8",
                "update_time": "20191106080000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "24": {
                "degree": "10",
                "update_time": "20191106090000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "25": {
                "degree": "11",
                "update_time": "20191106100000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "26": {
                "degree": "12",
                "update_time": "20191106110000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "27": {
                "degree": "12",
                "update_time": "20191106120000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "28": {
                "degree": "13",
                "update_time": "20191106130000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "29": {
                "degree": "13",
                "update_time": "20191106140000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "3": {
                "degree": "11",
                "update_time": "20191105120000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "30": {
                "degree": "13",
                "update_time": "20191106150000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "31": {
                "degree": "13",
                "update_time": "20191106160000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "32": {
                "degree": "13",
                "update_time": "20191106170000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "33": {
                "degree": "13",
                "update_time": "20191106180000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "34": {
                "degree": "13",
                "update_time": "20191106190000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "35": {
                "degree": "13",
                "update_time": "20191106200000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西风",
                "wind_power": "3"
            },
            "36": {
                "degree": "12",
                "update_time": "20191106210000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东南风",
                "wind_power": "3"
            },
            "37": {
                "degree": "12",
                "update_time": "20191106220000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "西南风",
                "wind_power": "3"
            },
            "38": {
                "degree": "12",
                "update_time": "20191106230000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "3"
            },
            "39": {
                "degree": "12",
                "update_time": "20191107000000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "4": {
                "degree": "12",
                "update_time": "20191105130000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "4"
            },
            "40": {
                "degree": "12",
                "update_time": "20191107010000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "41": {
                "degree": "12",
                "update_time": "20191107020000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "3"
            },
            "42": {
                "degree": "12",
                "update_time": "20191107030000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "43": {
                "degree": "11",
                "update_time": "20191107040000",
                "weather": "多云",
                "weather_code": "01",
                "weather_short": "多云",
                "wind_direction": "南风",
                "wind_power": "3"
            },
            "44": {
                "degree": "11",
                "update_time": "20191107050000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "3"
            },
            "45": {
                "degree": "10",
                "update_time": "20191107060000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "3"
            },
            "46": {
                "degree": "8",
                "update_time": "20191107070000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "4"
            },
            "47": {
                "degree": "6",
                "update_time": "20191107080000",
                "weather": "小雨",
                "weather_code": "07",
                "weather_short": "小雨",
                "wind_direction": "东风",
                "wind_power": "3"
            },
            "5": {
                "degree": "13",
                "update_time": "20191105140000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西北风",
                "wind_power": "3"
            },
            "6": {
                "degree": "13",
                "update_time": "20191105150000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西南风",
                "wind_power": "3"
            },
            "7": {
                "degree": "13",
                "update_time": "20191105160000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西南风",
                "wind_power": "3"
            },
            "8": {
                "degree": "13",
                "update_time": "20191105170000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西北风",
                "wind_power": "3"
            },
            "9": {
                "degree": "12",
                "update_time": "20191105180000",
                "weather": "阴",
                "weather_code": "02",
                "weather_short": "阴",
                "wind_direction": "西南风",
                "wind_power": "3"
            }
        },
        "forecast_24h": {
            "0": {
                "day_weather": "多云",
                "day_weather_code": "01",
                "day_weather_short": "多云",
                "day_wind_direction": "东北风",
                "day_wind_direction_code": "1",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "17",
                "min_degree": "7",
                "night_weather": "多云",
                "night_weather_code": "01",
                "night_weather_short": "多云",
                "night_wind_direction": "东南风",
                "night_wind_direction_code": "3",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-04"
            },
            "1": {
                "day_weather": "阴",
                "day_weather_code": "02",
                "day_weather_short": "阴",
                "day_wind_direction": "西北风",
                "day_wind_direction_code": "7",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "14",
                "min_degree": "8",
                "night_weather": "小雨",
                "night_weather_code": "07",
                "night_weather_short": "小雨",
                "night_wind_direction": "西风",
                "night_wind_direction_code": "6",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-05"
            },
            "2": {
                "day_weather": "小雨",
                "day_weather_code": "07",
                "day_weather_short": "小雨",
                "day_wind_direction": "西风",
                "day_wind_direction_code": "6",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "15",
                "min_degree": "5",
                "night_weather": "小雨",
                "night_weather_code": "07",
                "night_weather_short": "小雨",
                "night_wind_direction": "东风",
                "night_wind_direction_code": "2",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-06"
            },
            "3": {
                "day_weather": "多云",
                "day_weather_code": "01",
                "day_weather_short": "多云",
                "day_wind_direction": "东北风",
                "day_wind_direction_code": "1",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "16",
                "min_degree": "5",
                "night_weather": "多云",
                "night_weather_code": "01",
                "night_weather_short": "多云",
                "night_wind_direction": "西风",
                "night_wind_direction_code": "6",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-07"
            },
            "4": {
                "day_weather": "晴",
                "day_weather_code": "00",
                "day_weather_short": "晴",
                "day_wind_direction": "西北风",
                "day_wind_direction_code": "7",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "16",
                "min_degree": "5",
                "night_weather": "多云",
                "night_weather_code": "01",
                "night_weather_short": "多云",
                "night_wind_direction": "西北风",
                "night_wind_direction_code": "7",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-08"
            },
            "5": {
                "day_weather": "小雨",
                "day_weather_code": "07",
                "day_weather_short": "小雨",
                "day_wind_direction": "西风",
                "day_wind_direction_code": "6",
                "day_wind_power": "4",
                "day_wind_power_code": "1",
                "max_degree": "14",
                "min_degree": "6",
                "night_weather": "晴",
                "night_weather_code": "00",
                "night_weather_short": "晴",
                "night_wind_direction": "西南风",
                "night_wind_direction_code": "5",
                "night_wind_power": "4",
                "night_wind_power_code": "1",
                "time": "2019-11-09"
            },
            "6": {
                "day_weather": "多云",
                "day_weather_code": "01",
                "day_weather_short": "多云",
                "day_wind_direction": "西南风",
                "day_wind_direction_code": "5",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "13",
                "min_degree": "6",
                "night_weather": "晴",
                "night_weather_code": "00",
                "night_weather_short": "晴",
                "night_wind_direction": "西南风",
                "night_wind_direction_code": "5",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-10"
            },
            "7": {
                "day_weather": "晴",
                "day_weather_code": "00",
                "day_weather_short": "晴",
                "day_wind_direction": "西南风",
                "day_wind_direction_code": "5",
                "day_wind_power": "3",
                "day_wind_power_code": "0",
                "max_degree": "14",
                "min_degree": "4",
                "night_weather": "晴",
                "night_weather_code": "00",
                "night_weather_short": "晴",
                "night_wind_direction": "西南风",
                "night_wind_direction_code": "5",
                "night_wind_power": "3",
                "night_wind_power_code": "0",
                "time": "2019-11-11"
            }
        },
        "index": {
            "airconditioner": {
                "detail": "您将感到很舒适,一般不需要开启空调。",
                "info": "较少开启",
                "name": "空调开启"
            },
            "allergy": {
                "detail": "天气条件极不易诱发过敏,可放心外出,享受生活。",
                "info": "极不易发",
                "name": "过敏"
            },
            "carwash": {
                "detail": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。",
                "info": "不宜",
                "name": "洗车"
            },
            "chill": {
                "detail": "温度未达到风寒所需的低温,稍作防寒准备即可。",
                "info": "无",
                "name": "风寒"
            },
            "clothes": {
                "detail": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。",
                "info": "较冷",
                "name": "穿衣"
            },
            "cold": {
                "detail": "将有一次强降温过程,极易发生感冒,请特别注意增加衣服保暖防寒。",
                "info": "极易发",
                "name": "感冒"
            },
            "comfort": {
                "detail": "白天天气阴沉,您会感觉偏冷,不很舒适,请注意添加衣物,以防感冒。",
                "info": "较不舒适",
                "name": "舒适度"
            },
            "diffusion": {
                "detail": "气象条件较不利于空气污染物稀释、扩散和清除。",
                "info": "较差",
                "name": "空气污染扩散条件"
            },
            "dry": {
                "detail": "阴天,路面比较干燥,路况较好。",
                "info": "干燥",
                "name": "路况"
            },
            "drying": {
                "detail": "天气阴沉,不利于水分的迅速蒸发,不太适宜晾晒。若需要晾晒,请尽量选择通风的地点。",
                "info": "不太适宜",
                "name": "晾晒"
            },
            "fish": {
                "detail": "天气不好,有风,不适合垂钓。",
                "info": "不宜",
                "name": "钓鱼"
            },
            "heatstroke": {
                "detail": "天气舒适,对易中暑人群来说非常友善。",
                "info": "无中暑风险",
                "name": "中暑"
            },
            "makeup": {
                "detail": "皮肤易缺水,用润唇膏后再抹口红,用保湿型霜类化妆品。",
                "info": "保湿",
                "name": "化妆"
            },
            "mood": {
                "detail": "天气阴沉,会感觉莫名的压抑,情绪低落,此时将所有的悲喜都静静地沉到心底,在喧嚣的尘世里,感受片刻恬淡的宁静。",
                "info": "较差",
                "name": "心情"
            },
            "morning": {
                "detail": "早晨气象条件较适宜晨练,但天气阴沉,风力稍大,请选择合适的地点晨练。",
                "info": "较适宜",
                "name": "晨练"
            },
            "sports": {
                "detail": "有降水,推荐您在室内进行健身休闲运动;若坚持户外运动,须注意保暖并携带雨具。",
                "info": "较不宜",
                "name": "运动"
            },
            "sunglasses": {
                "detail": "白天天空阴沉,但太阳辐射较强,建议佩戴透射比1级且标注UV380-UV400的浅色太阳镜",
                "info": "必要",
                "name": "太阳镜"
            },
            "sunscreen": {
                "detail": "属弱紫外辐射天气,长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。",
                "info": "弱",
                "name": "防晒"
            },
            "time": "20191105",
            "tourism": {
                "detail": "天气较好,温度适宜,总体来说还是好天气哦,这样的天气适宜旅游,您可以尽情地享受大自然的风光。",
                "info": "适宜",
                "name": "旅游"
            },
            "traffic": {
                "detail": "阴天,路面干燥,交通气象条件良好,车辆可以正常行驶。",
                "info": "良好",
                "name": "交通"
            },
            "ultraviolet": {
                "detail": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。",
                "info": "最弱",
                "name": "紫外线强度"
            },
            "umbrella": {
                "detail": "阴天,但降水概率很低,因此您在出门的时候无须带雨伞。",
                "info": "不带伞",
                "name": "雨伞"
            }
        },
        "limit": {
            "tail_number": "",
            "time": ""
        },
        "tips": {
            "observe": {
                "0": "光芒透过云缝,洒向大地~",
                "1": "现在的温度比较凉爽~"
            }
        }
    },
    "message": "OK",
    "status": 200
}
发布了58 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42846555/article/details/102914058
今日推荐