彩云天气API 获取实时及当日天气等内容

news/2024/10/15 20:16:53
import requests# 用户输入的秘钥和经纬度(以逗号分隔)
api_key = ""  # 这里输入用户自己的秘钥
location = ""  # 输入用户的经纬度# 抓取天气信息的函数
def get_weather_info(api_key, location):try:# 实时天气APIrealtime_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/realtime"# 当日天气APIdaily_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/daily?dailysteps=1"# 获取实时天气数据realtime_response = requests.get(realtime_url)# 获取当日天气数据daily_response = requests.get(daily_url)if realtime_response.status_code == 200 and daily_response.status_code == 200:realtime_data = realtime_response.json().get('result', {}).get('realtime', {})daily_data = daily_response.json().get('result', {}).get('daily', {})# 提取实时天气数据temperature = realtime_data.get('temperature')humidity = realtime_data.get('humidity')skycon = realtime_data.get('skycon')wind = realtime_data.get('wind', {})apparent_temperature = realtime_data.get('apparent_temperature')precipitation = realtime_data.get('precipitation', {})local_precip = precipitation.get('local', {})nearest_precip = precipitation.get('nearest', {})# 提取当日天气数据daily_temp = daily_data.get('temperature', [{}])[0]daily_humidity = daily_data.get('humidity', [{}])[0]daily_skycon = daily_data.get('skycon', [{}])[0].get('value', '未知')# 天气状况翻译skycon_translation = {"CLEAR_DAY": "晴天","CLEAR_NIGHT": "晴夜","PARTLY_CLOUDY_DAY": "多云","PARTLY_CLOUDY_NIGHT": "多云夜晚","CLOUDY": "阴天","LIGHT_HAZE": "轻度雾霾","MODERATE_HAZE": "中度雾霾","HEAVY_HAZE": "重度雾霾","LIGHT_RAIN": "小雨","MODERATE_RAIN": "中雨","HEAVY_RAIN": "大雨","STORM_RAIN": "暴雨","FOG": "雾","LIGHT_SNOW": "小雪","MODERATE_SNOW": "中雪","HEAVY_SNOW": "大雪","STORM_SNOW": "暴雪","DUST": "浮尘","SAND": "沙尘","WIND": "大风"}# 翻译天气状况skycon_desc = skycon_translation.get(skycon, "未知天气状况")# 构建输出字符串weather_info = (f"实时天气情况: {skycon_desc}\n"f"实时温度: {round(temperature)}°C (体感: {round(apparent_temperature)}°C)\n"f"每秒风速: {wind.get('speed')}米\n")# 判断降水状况if local_precip.get('intensity', 0) == 0 and nearest_precip.get('distance', 0) > 10000:weather_info += "降水监测: 目前无降水(雷达显示最近降水距离超过10公里)"else:weather_info += "降水监测: 雷达显示10公里区域内存在降水"# 加入当日天气信息(只显示温度、湿度和天气状况)weather_info += (f"\n当日天气情况: {skycon_translation.get(daily_skycon, '未知')}\n"f"当日温度: {round(daily_temp.get('min'))}°C ~ {round(daily_temp.get('max'))}°C\n"f"当日湿度: {round(int(daily_humidity.get('min') * 100))} % ~ {round(int(daily_humidity.get('max') * 100))} %\n")return weather_infoelse:return "无法获取天气数据。"except requests.exceptions.RequestException as e:print(f"抓取天气信息失败: {e}")return None# 主程序入口
if __name__ == "__main__":# 调用天气信息函数weather_result = get_weather_info(api_key, location)if weather_result:print("公司总部天气信息:\n",weather_result)else:print("未能提取到天气信息。")

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/71964.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

习题3.2

def X(n): # 差分方程的解 return 2 * (-1)**(n + 1) n_values = [0, 1, 2, 3, 4, 5] for n in n_values: print(f"X({n}) = {X(n)}")print("学号:3008") 结果如下

习题2.9

import sympy as sp # 定义变量 x, y = sp.symbols(x y) # 定义方程组 equation1 = sp.Eq(x**2 - y - x, 3) equation2 = sp.Eq(x + 3*y, 2) # 解方程组 solutions = sp.solve((equation1, equation2), (x, y), dict=True) print("符号解:") for sol …

习题2.10

from scipy.integrate import quad import numpy as np # 第一部分:抛物线旋转体(修正后) def V1_quad(y): return np.pi * (4*y - y**2) V1_corrected, _ = quad(V1_quad, 1, 3) # 第二部分保持不变 V2 = 0.5 * (4/3) * np.pi * 2**3 - (1/3) * np.pi * 2**2 * 1…

开发者门户是什么?为什么企业需要它?

随着企业规模的扩大,其基础设施、服务以及API的复杂性往往增长得更为迅速。在这种增长背景下,了解现有资源并合理利用这些资源变得愈发困难。尤其是当你涉及到外部开发者和第三方应用开发者时,创建一个了解和交互基础设施、服务和API的中央平台能够节省时间并简化入门流程。…

第五周(10.8-

代码题: 1、给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 题解:如果等于nums[middle],返回middle;否则返回left或者low。 2、在排序数组中查找target的开始位置和结束位置。 二分法不可能会漏…

习题2.6

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 模拟高程数据(假设数据已经过某种方式插值或生成) # 这里我们创建一个简单的40x50网格,并填充随机高程值 x = np.linspace(0, 43.65, 40) y = np.linspace(0, 58…

习题2.7(2)

import numpy as np # 定义系数矩阵A和常数项向量b A = np.array([[2, 3, 1], [1, -2, 4], [3, 8, -2], [4, -1, 9]]) b = np.array([4, -5, 13, -6]) # 使用numpy的lstsq函数求解最小二乘解 # 对于这个特定的问题,由于方程数和未知数数量相同,且没有矛盾,lstsq将…

习题2.4

import numpy as np import matplotlib.pyplot as plt # 定义x的范围 x = np.linspace(-10, 10, 400) # 创建一个2行3列的子图布局 fig, axs = plt.subplots(2, 3, figsize=(12, 8)) # 遍历每个子图 for k, ax in enumerate(axs.flat, start=1): y = k * x**2 + 2*…