python教程

Python获取彩云天气实时天气API源码

我的站长站 2024-10-16 人阅读
import requests
 # 用户输入的秘钥和经纬度(以逗号分隔)
api_key = ""  # 这里输入用户自己的秘钥
location = ""  # 输入用户的经纬度
 
# 抓取天气信息的函数
def get_weather_info(api_key, location):
    try:
        # 实时天气API
        realtime_url = f"https://api.caiyunapp.com/v2.6/{api_key}/{location}/realtime"
        # 当日天气API
        daily_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_info
        else:
            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("未能提取到天气信息。")
相关推荐
  • Python获取
  • 天气API源码
  • API源码
  • Python获取彩云天气实时天气API源码

    import requests # 用户输入的秘钥和经纬度(以逗号分隔)api_key = "" # 这里输入用户自己的秘钥location = "" # 输入用户的经纬度 # 抓取天气信息的函数def get_weather_info(api_key, location): try: # 实时天气API realtime_ur...

    python教程 3 1天前
  • python批量获取百度云用户份信息

    python批量获取百度云用户份信息,未编译直接发代码,能用到的自行修改,以下代码适合于在文件夹中多个文件批量提取用户信息。from aip import AipOcrimport osimport imghdrimport sys#import filetype #代{过}{滤}理服务器访问设置设置#os.environ["ht...

    python教程 19 8个月前
  • 批量获取网站百度谷歌360权重Python源码

    批量获取网站百度谷歌360权重Python源码,采用随机ua,批量抓取网站的权重。 import requestsfrom bs4 import BeautifulSoupimport timeimport random # 读取文件内容with open('www.txt', 'r') as f: content = f.read() # 提取域...

    python教程 23 1年前
  • Python获取彩云天气实时天气API源码

    import requests # 用户输入的秘钥和经纬度(以逗号分隔)api_key = "" # 这里输入用户自己的秘钥location = "" # 输入用户的经纬度 # 抓取天气信息的函数def get_weather_info(api_key, location): try: # 实时天气API realtime_ur...

    python教程 3 1天前
  • Python爬虫抓取中国天气并发送到微信

    Python爬取中国天气随便点进一个城市的详细天气预报,这里以北京为例。按照惯例开下F12。这种实时更新的界面一般是通过ajax传入json文件实现的,打开network选项卡刷新验证一下。Python天气爬虫截图url = "https://d1.weather.com.cn/sk_2d/101010100.h...

    python教程 145 3年前
  • PHP获取城市天气API接口源码

    PHP获取城市天气API接口源码,接口调用的是微鲤的接口,2020年9月1日 10:11:07测试接口还可以正常使用。API查询接口代码<?php function tian(){ $city=$_GET[&#39;city&#39;]; $url="https://wthrcdn.etouch.cn/weather_mini?city=".$city; ...

    php教程 139 4年前
  • PHP智云V1.3全能API接口网站源码
    PHP智云V1.3全能API接口网站源码

    智云全能API接口PHP源码V1.3版本接口数据由智云平台开发及整理源码直接上传即可访问,无需数据库,不支持上传二级目录访问!源码上传后请访问:你的域名/inde.html(可以将inde.html重新修改其他名称访问)...

    php源码 553 4年前
  • 2020年最新HTML+API版防红网站模板
    2020年最新HTML+API版防红网站模板

    2020年最新HTML+API版防红网站模板,HTML+JS+API框架,打开即可使用,不需要任何环境。支持多种防红方式,多种短域名后缀。

    html模板 464 4年前
  • HTML+API美腿zipa图片瀑布流网站模板
    HTML+API美腿zipa图片瀑布流网站模板

    HTML+API美腿zipa图片瀑布流网站模板,图片采集与自葫芦侠,流量不走本地,虚拟空间都能部署,适合引流请在下载后24小时内删除,否则一切法律后果请自行承担...

    html模板 680 4年前
最新更新