查看: 86|回复: 0

[工具分享] 彩云天气API 获取实时及当日天气等内容

[复制链接]
累计签到:304 天
连续签到:2 天

806

主题

-22

回帖

1万

积分

热心会员

名望
126
星币
4240
星辰
7
好评
254
发表于 7 天前 | 显示全部楼层 |阅读模式

注册登录后全站资源免费查看下载

您需要 登录 才可以下载或查看,没有账号?立即注册

×


  1. import requests
  2. # 用户输入的秘钥和经纬度(以逗号分隔)
  3. api_key = ""  # 这里输入用户自己的秘钥
  4. location = ""  # 输入用户的经纬度

  5. # 抓取天气信息的函数
  6. def get_weather_info(api_key, location):
  7.     try:
  8.         # 实时天气API
  9.         realtime_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/realtime"
  10.         # 当日天气API
  11.         daily_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/daily?dailysteps=1"

  12.         # 获取实时天气数据
  13.         realtime_response = requests.get(realtime_url)
  14.         # 获取当日天气数据
  15.         daily_response = requests.get(daily_url)

  16.         if realtime_response.status_code == 200 and daily_response.status_code == 200:
  17.             realtime_data = realtime_response.json().get('result', {}).get('realtime', {})
  18.             daily_data = daily_response.json().get('result', {}).get('daily', {})

  19.             # 提取实时天气数据
  20.             temperature = realtime_data.get('temperature')
  21.             humidity = realtime_data.get('humidity')
  22.             skycon = realtime_data.get('skycon')
  23.             wind = realtime_data.get('wind', {})
  24.             apparent_temperature = realtime_data.get('apparent_temperature')
  25.             precipitation = realtime_data.get('precipitation', {})
  26.             local_precip = precipitation.get('local', {})
  27.             nearest_precip = precipitation.get('nearest', {})

  28.             # 提取当日天气数据
  29.             daily_temp = daily_data.get('temperature', [{}])[0]
  30.             daily_humidity = daily_data.get('humidity', [{}])[0]
  31.             daily_skycon = daily_data.get('skycon', [{}])[0].get('value', '未知')

  32.             # 天气状况翻译
  33.             skycon_translation = {
  34.                 "CLEAR_DAY": "晴天",
  35.                 "CLEAR_NIGHT": "晴夜",
  36.                 "PARTLY_CLOUDY_DAY": "多云",
  37.                 "PARTLY_CLOUDY_NIGHT": "多云夜晚",
  38.                 "CLOUDY": "阴天",
  39.                 "LIGHT_HAZE": "轻度雾霾",
  40.                 "MODERATE_HAZE": "中度雾霾",
  41.                 "HEAVY_HAZE": "重度雾霾",
  42.                 "LIGHT_RAIN": "小雨",
  43.                 "MODERATE_RAIN": "中雨",
  44.                 "HEAVY_RAIN": "大雨",
  45.                 "STORM_RAIN": "暴雨",
  46.                 "FOG": "雾",
  47.                 "LIGHT_SNOW": "小雪",
  48.                 "MODERATE_SNOW": "中雪",
  49.                 "HEAVY_SNOW": "大雪",
  50.                 "STORM_SNOW": "暴雪",
  51.                 "DUST": "浮尘",
  52.                 "SAND": "沙尘",
  53.                 "WIND": "大风"
  54.             }

  55.             # 翻译天气状况
  56.             skycon_desc = skycon_translation.get(skycon, "未知天气状况")

  57.             # 构建输出字符串
  58.             weather_info = (
  59.                 f"实时天气情况: {skycon_desc}\n"
  60.                 f"实时温度: {round(temperature)}°C (体感: {round(apparent_temperature)}°C)\n"
  61.                 f"每秒风速: {wind.get('speed')}米\n"
  62.             )

  63.             # 判断降水状况
  64.             if local_precip.get('intensity', 0) == 0 and nearest_precip.get('distance', 0) > 10000:
  65.                 weather_info += "降水监测: 目前无降水(雷达显示最近降水距离超过10公里)"
  66.             else:
  67.                 weather_info += "降水监测: 雷达显示10公里区域内存在降水"

  68.             # 加入当日天气信息(只显示温度、湿度和天气状况)
  69.             weather_info += (
  70.                 f"\n当日天气情况: {skycon_translation.get(daily_skycon, '未知')}\n"
  71.                 f"当日温度: {round(daily_temp.get('min'))}°C ~ {round(daily_temp.get('max'))}°C\n"
  72.                 f"当日湿度: {round(int(daily_humidity.get('min') * 100))} % ~ {round(int(daily_humidity.get('max') * 100))} %\n"
  73.             )

  74.             return weather_info
  75.         else:
  76.             return "无法获取天气数据。"

  77.     except requests.exceptions.RequestException as e:
  78.         print(f"抓取天气信息失败: {e}")
  79.         return None

  80. # 主程序入口
  81. if __name__ == "__main__":

  82.     # 调用天气信息函数
  83.     weather_result = get_weather_info(api_key, location)

  84.     if weather_result:
  85.         print("公司总部天气信息:\n",weather_result)
  86.     else:
  87.         print("未能提取到天气信息。")
复制代码
实际很多内容 我只提取我自己需要的

注册免/费API:http://caiyunai.com/api/weather_api.html#api


最终放公司钉钉每日推送效果



默认签名:偏爱是我家,发展靠大家! 社区反馈邮箱Mail To:service@pai.al或paijishu@outlook.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网--教开服-游戏源码

偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网-游戏源码

Powered by Discuz! X3.5

GMT+8, 2024-10-23 01:56 , Processed in 0.083169 second(s), 31 queries .

快速回复 返回顶部 返回列表