天气预报查询 API、历史天气查询 API 接口使用示例【源码可用】
福利彩蛋:没有好玩的 API 接口?上百款免费接口等你来,免费 API,免费 API 大全
一、免费天气预报查询 API
通过城市名称或城市 ID 查询天气预报情况
简单快捷,根据个人需求进行开发
接口请求流程:
- 1、获取免费天气预报查询 API AppKey
- 2、替换示例代码中的”appKey”
- 3、执行脚本
示例代码:
import random
import requests
import json
appKey='appKey'
class ApiRequest(object):
def __init__(self,appKey):
self.appKey = appKey
def verify(self,city):
headers ={
"Content-Type": "application/x-www-form-urlencoded"}
params ={
"city": city,
"key": self.appKey,
}
print(params)
resp = requests.get("http://apis.juhe.cn/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': '2023-05-22', 'temperature': '13/21℃', 'weather': '小雨转多云', 'wid': {'day': '07', 'night': '01'}, 'direct': '北风转西北风'}, {'date': '2023-05-23', 'temperature': '16/26℃', 'weather': '多云转小雨', 'wid': {'day': '01', 'night': '07'}, 'direct': '东风转东南风'}, {'date': '2023-05-24', 'temperature': '17/22℃', 'weather': '小雨转多云', 'wid': {'day': '07', 'night': '01'}, 'direct': '东南风'}, {'date': '2023-05-25', 'temperature': '21/30℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '东南风'}, {'date': '2023-05-26', 'temperature': '22/30℃', 'weather': '多云', 'wid': {'day': '01', 'night': '01'}, 'direct': '南风转东南风'}]}, 'error_code': 0}
return resp_json
city ='苏州'
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": "2023-05-22",
"temperature": "13/21℃",
"weather": "小雨转多云",
"wid": {
"day": "07",
"night": "01"
},
"direct": "北风转西北风"
},
{
"date": "2023-05-23",
"temperature": "16/26℃",
"weather": "多云转小雨",
"wid": {
"day": "01",
"night": "07"
},
"direct": "东风转东南风"
},
{
"date": "2023-05-24",
"temperature": "17/22℃",
"weather": "小雨转多云",
"wid": {
"day": "07",
"night": "01"
},
"direct": "东南风"
},
{
"date": "2023-05-25",
"temperature": "21/30℃",
"weather": "多云",
"wid": {
"day": "01",
"night": "01"
},
"direct": "东南风"
},
{
"date": "2023-05-26",
"temperature": "22/30℃",
"weather": "多云",
"wid": {
"day": "01",
"night": "01"
},
"direct": "南风转东南风"
}
]
},
"error_code": 0
}
二、历史天气查询 API
根据城市 ID 和日期查询历史天气信息,日期支持从 2011-01-01 开始。注:个别地区个别日期数据记录可能会不存在
简单快捷,根据个人需求进行开发
接口请求流程:
- 1、免费获取历史天气查询 API AppKey
- 2、替换示例代码中的”appKey”
- 3、执行脚本
获取支持的省份和城市列表、Id
示例代码:
import random
import requests
import json
appKey='appKey'
class ApiRequest(object):
def __init__(self,appKey):
self.appKey = appKey
def verify(self,city):
headers ={
"Content-Type": "application/x-www-form-urlencoded"}
params ={
"city_id": city,
"key": self.appKey,
"weather_date":'2022-01-01' # 日期,格式:2017-07-15,日期不能大于等于今日日期
}
print(params)
resp = requests.get("http://v.juhe.cn/historyWeather/weather",params,headers=headers)
resp_json = json.loads(resp.text)
# {'reason': '查询成功', 'result': {'city_id': '1157', 'city_name': '苏州', 'weather_date': '2022-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_json
city ='1157' # 苏州
pet = ApiRequest(appKey)
rest = pet.verify(city)
print(rest)
返回参数说明:”>
请求返回结果:
{
"reason": "查询成功",
"result": {
"city_id": "1157",
"city_name": "苏州",
"weather_date": "2022-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 大全
今天的文章免费天气预报查询 API、历史天气查询 API 接口使用示例【源码可用】分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/61729.html