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

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

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

一、免费天气预报查询 API

通过城市名称或城市 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":    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 开始。注:个别地区个别日期数据记录可能会不存在
简单快捷,根据个人需求进行开发
接口请求流程:

获取支持的省份和城市列表、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 大全

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,天气预报API免费接口使用步骤如下: 1. 首先,需要免费获取历史天气查询API的AppKey。\[2\] 2. 将获取到的AppKey替换示例代码中的"appKey"。\[3\] 3. 执行脚本,即可进行天气预报查询。\[3\] 示例代码中的`ApiRequest`类是一个封装了天气查询功能的类,其中的`verify`方法用于发送请求并获取天气信息。你可以根据自己的需求进行开发和调整。\[3\] 需要注意的是,示例代码中的`city_id`参数用于指定城市的ID,`weather_date`参数用于指定查询的日期。日期的格式为"YYYY-MM-DD",且不能大于等于今日日期。\[3\] 以上是关于天气预报API免费接口使用方法。希望对你有帮助! #### 引用[.reference_title] - *1* [免费天气预报接口](https://blog.csdn.net/weichangIT/article/details/80255206)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [免费天气预报查询 API历史天气查询 API 接口使用示例源码可用】](https://blog.csdn.net/JAVASOFT008/article/details/130812391)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值