其他分享
首页 > 其他分享> > 天气情况的获取

天气情况的获取

作者:互联网

函数获取使用高德天气接口

import requests

爬取数据

def read_location(name):
    centerurl = "https://restapi.amap.com/v3/config/district?keywords="
    centerurl2 = "&subdistrict=2&key=ecbbca20adc1bcb1e1416c23a8929921"
    datacenter = requests.get(centerurl+name+centerurl2)
    ss=datacenter.json()
    saa = ss["districts"]
    center = saa[0]["center"]
    return center

def read_locationa(code):
    centerurl = "https://restapi.amap.com/v3/weather/weatherInfo?key=ecbbca20adc1bcb1e1416c23a8929921&extensions=all&city="
    centerurl2 = "https://restapi.amap.com/v3/weather/weatherInfo?key=ecbbca20adc1bcb1e1416c23a8929921&city="
    datacenter = requests.get(centerurl+code)
    datacenter2 = requests.get(centerurl2+code)
    ss=datacenter.json()
    ss2 = datacenter2.json()
    ssaw=[]
    ssa2 = {}
    sa =ss["forecasts"][0]
    for it in sa['casts']:
        ssa = {}
        ssa["dayweather"] = it["dayweather"]
        ssa["nightweather"] = it["nightweather"]
        ssa["daytemp"] = it["daytemp"]
        ssa["nighttemp"] = it["nighttemp"]
        ssa["daypower"] = it["daypower"]
        ssa["nightpower"] = it["nightpower"]
        ssaw.append(ssa)
    ssa2["weather"]=ss2["lives"][0]["weather"]
    ssa2["temperature"]=ss2["lives"][0]["temperature"]
    ssa2["windpower"]=ss2["lives"][0]["windpower"]
    jun = []
    for it in ssaw:
        jun.append(yuceday(it))
    sum = 0
    for it1 in jun:
        sum +=it1
    sum = sum/4
    sum +=dangday(ssa2)*2
    ssa2['sum'] = sum
    return ssa2

 

标签:天气情况,sum,ssa2,ss2,获取,weather,centerurl,ssa
来源: https://www.cnblogs.com/weidewozi/p/14908234.html