200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 天气预报查询 API 历史天气查询 API 接口使用示例【源码可用】

天气预报查询 API 历史天气查询 API 接口使用示例【源码可用】

时间:2022-11-02 03:04:04

相关推荐

天气预报查询 API 历史天气查询 API 接口使用示例【源码可用】

天气预报查询 API、历史天气查询 API 接口使用示例【源码可用】

福利彩蛋:没有好玩的 API 接口?上百款免费接口等你来,免费 API,免费 API 大全

一、免费天气预报查询 API

通过城市名称或城市 ID 查询天气预报情况

简单快捷,根据个人需求进行开发

接口请求流程:

1、获取免费天气预报查询 API AppKey2、替换示例代码中的"appKey"3、执行脚本

示例代码:

import randomimport requestsimport jsonappKey='appKey'class ApiRequest(object):def __init__(self,appKey):self.appKey = appKeydef verify(self,city):headers ={"Content-Type": "application/x-www-form-urlencoded"}params ={"city": city,"key": self.appKey,}print(params)resp = requests.get("/simpleWeather/query",params,headers=headers)resp_json = json.loads(resp.text)# {'reason': '查询成功!', 'result': {'city': '苏州', 'realtime': {'temperature': '16', 'humidity': '83', 'info': '阴', 'wid': '02', 'direct': '东北风', 'power': '4级', 'aqi': '115'}, 'future': [{'date': '-05-22', 'temperature': '13/21℃', 'weather': '小雨转多云', 'wid': {'day': '07', 'night': '01'}, 'direct': '北风转西北风'}, {'date': '-05-23', 'temperature': '16/26℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东风转东南风'}, {'date': '-05-24', 'temperature': '17/22℃', 'weather': '小雨转多云', 'wid': {'day': '07', 'night': '01'}, 'direct': '东南风'}, {'date': '-05-25', 'temperature': '21/30℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '东南风'}, {'date': '-05-26', 'temperature': '22/30℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '南风转东南风'}]}, 'error_code': 0}return resp_jsoncity ='苏州'pet = ApiRequest(appKey)rest = pet.verify(city)print(rest)

请求返回结果:

{"reason": "查询成功!","result": {"city": "苏州","realtime": {"temperature": "16","humidity": "83","info": "阴","wid": "02","direct": "东北风","power": "4级","aqi": "115"},"future": [{"date": "-05-22","temperature": "13/21℃","weather": "小雨转多云","wid": {"day": "07","night": "01"},"direct": "北风转西北风"},{"date": "-05-23","temperature": "16/26℃","weather": "多云转小雨","wid": {"day": "01","night": "07"},"direct": "东风转东南风"},{"date": "-05-24","temperature": "17/22℃","weather": "小雨转多云","wid": {"day": "07","night": "01"},"direct": "东南风"},{"date": "-05-25","temperature": "21/30℃","weather": "多云","wid": {"day": "01","night": "01"},"direct": "东南风"},{"date": "-05-26","temperature": "22/30℃","weather": "多云","wid": {"day": "01","night": "01"},"direct": "南风转东南风"}]},"error_code": 0}

二、历史天气查询 API

根据城市 ID 和日期查询历史天气信息,日期支持从 -01-01 开始。注:个别地区个别日期数据记录可能会不存在

简单快捷,根据个人需求进行开发

接口请求流程:

1、免费获取历史天气查询 API AppKey2、替换示例代码中的"appKey"3、执行脚本

获取支持的省份和城市列表、Id

示例代码:

import randomimport requestsimport jsonappKey='appKey'class ApiRequest(object):def __init__(self,appKey):self.appKey = appKeydef verify(self,city):headers ={"Content-Type": "application/x-www-form-urlencoded"}params ={"city_id": city,"key": self.appKey,"weather_date":'-01-01' # 日期,格式:-07-15,日期不能大于等于今日日期}print(params)resp = requests.get("/historyWeather/weather",params,headers=headers)resp_json = json.loads(resp.text)# {'reason': '查询成功', 'result': {'city_id': '1157', 'city_name': '苏州', 'weather_date': '-01-01', 'day_weather': '多云', 'night_weather': '多云', 'day_temp': '10℃', 'night_temp': '1℃', 'day_wind': '东南风', 'day_wind_comp': '3-4级', 'night_wind': '西风', 'night_wind_comp': '<3级', 'day_weather_id': '01', 'night_weather_id': '01'}, 'error_code': 0}return resp_jsoncity ='1157' # 苏州pet = ApiRequest(appKey)rest = pet.verify(city)print(rest)

请求返回结果:

{"reason": "查询成功","result": {"city_id": "1157","city_name": "苏州","weather_date": "-01-01","day_weather": "多云","night_weather": "多云","day_temp": "10℃","night_temp": "1℃","day_wind": "东南风","day_wind_comp": "3-4级","night_wind": "西风","night_wind_comp": "<3级","day_weather_id": "01","night_weather_id": "01"},"error_code": 0}

福利彩蛋:没有好玩的 API 接口?上百款免费接口等你来,免费 API,免费 API 大全

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。