全国免费天气预报查询
阿里云全国免费天气预报查询网址:https://market.aliyun.com/products/57126001/cmapi014123.html?spm=5176.11065268.1996646101.searchclickresult.32d61062VCtKFz#sku=yuncode812300000
注意代码29行输入阿里云的appcode,需要购买
代码
import urllib.request, json
"""
type 输入的坐标类型: 1:GPS设备获取的角度坐标; 2:GPS获取的米制坐标、sogou地图所用坐标;
3:google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
4:3中列表地图坐标对应的米制坐标 5:百度地图采用的经纬度坐标
6:百度地图采用的米制坐标 7:mapbar地图坐标; 8:51地图坐标
1为需要,0为不需要。
need3HourForcast 是否需要当天每3/6小时一次的天气预报列表
needAlarm 是否需要天气预警
needIndex 是否需要返回指数数据,比如穿衣指数、紫外线指数等
needMoreDay 是否需要返回7天数据中的后4天
"""
def get_weather(
wd, jd, type=5, need3HourForcast=0, needAlarm=0, needIndex=0, needMoreDay=0
):
try:
host = "http://saweather.market.alicloudapi.com"
path = "/gps-to-weather"
method = "GET"
appcode = "输入你自己的APPCode"
querys = "from={}&lat={}&lng={}&need3HourForcast={}&needAlarm={}&needIndex={}&needMoreDay={}".format(
type, wd, jd, need3HourForcast, needAlarm, needIndex, needMoreDay
)
url = host + path + "?" + querys
request = urllib.request.Request(url)
request.add_header("Authorization", "APPCODE " + appcode)
response = urllib.request.urlopen(request)
content = response.read()
if content:
text = content.decode(encoding="utf-8", errors="strict")
my_data = json.loads(text)
if my_data["showapi_res_code"] == 0:
my_data["showapi_res_body"]["code"] = 200
return ("查询成功", my_data["showapi_res_body"])
else:
my_data["code"] = 404
return ("返回状态码异常", my_data)
except Exception as e:
res = {}
res["code"] = 555
res["error"] = "经纬度不正确"
res["info"] = "HTTP Error 555: Unknown"
return res
print(get_weather(40.242266, 116.2278))