Qt 从中央气象局——天气预报网页API获取天气预报数据

文章讲述了如何使用Qt编程语言解析NMC中国气象局的API接口,获取包括省份、城市列表、天气详情及历史预报等详细信息,避免了注册和数据限制问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:数据版权归中央气象局所有,仅供学习交流之用。本文内容若有侵权,请联系我删除~

前言

调研了很天气平台,发现都需要注册,注册后通过key来uri访问, 虽然便捷简单,但是像高德这种还有刷新次数限制,并且通过平台获取到数据并不是很完整的,仅仅就是天气预测的基本信息,没有天气预报网页API那样丰富

需要获取的URL

1 获取相关接口

通过解析html页面后,发现有如下3个接口:

  • 省份获取:http://www.nmc.cn/rest/province
  • 城市获取:http://www.nmc.cn/rest/province/{省份代码}
  • 城市天气预报获取:http://www.nmc.cn/rest/weather?stationid={城市代码}

2 省份获取接口数据

接口:http://www.nmc.cn/rest/province

返回数据

[
    {"code":"ABJ","name":"北京市","url":"/publish/forecast/ABJ.html"},
    {"code":"ATJ","name":"天津市","url":"/publish/forecast/ATJ.html"},
    {"code":"AHE","name":"河北省","url":"/publish/forecast/AHE.html"},
    {"code":"ASC","name":"四川省","url":"/publish/forecast/ASC.html"},
    ...
    ...
    {"code":"ATW","name":"台湾省","url":"/publish/forecast/ATW.html"}
]

注意:这里的code键的值为城市获取的参数,如北京的参数为:ABJ

3 城市获取接口数据

接口:http://www.nmc.cn/rest/province/ABJ

返回数据

[
    {
        "code": "54511",
        "province": "北京市",
        "city": "北京",
        "url": "/publish/forecast/ABJ/beijing.html"
    },
    {
        "code": "54499",
        "province": "北京市",
        "city": "昌平",
        "url": "/publish/forecast/ABJ/changping.html"
    },
  	...
  	...
    {
        "code": "54406",
        "province": "北京市",
        "city": "延庆",
        "url": "/publish/forecast/ABJ/yanqing.html"
    }
]

4 城市天气预报获取接口数据

接口:http://www.nmc.cn/rest/weather?stationid=54511
返回数据

{
    "msg": "success",
    "code": 0,
    "data": {
        "real": {
            "station": {
                "code": "54511",
                "province": "北京市",
                "city": "北京",
                "url": "/publish/forecast/ABJ/beijing.html"
            },
            "publish_time": "2024-02-27 16:15",
            "weather": {
                "temperature": 5.5,
                "temperatureDiff": 1.1,
                "airpressure": 9999,
                "humidity": 37,
                "rain": 0,
                "rcomfort": 42,
                "icomfort": -2,
                "info": "多云",
                "img": "1",
                "feelst": 2.1
            },
            "wind": {
                "direct": "西南风",
                "degree": 261,
                "power": "微风",
                "speed": 2.7
            },
            "warn": {
                "alert": "9999",
                "pic": "9999",
                "province": "9999",
                "city": "9999",
                "url": "9999",
                "issuecontent": "9999",
                "fmeans": "9999",
                "signaltype": "9999",
                "signallevel": "9999",
                "pic2": "9999"
            }
        },
        "predict": {
            "station": {
                "code": "54511",
                "province": "北京市",
                "city": "北京",
                "url": "/publish/forecast/ABJ/beijing.html"
            },
            "publish_time": "2024-02-27 12:00",
            "detail": [
                {
                    "date": "2024-02-27",
                    "pt": "2024-02-27 12:00",
                    "day": {
                        "weather": {
                            "info": "多云",
                            "img": "1",
                            "temperature": "6"
                        },
                        "wind": {
                            "direct": "南风",
                            "power": "微风"
                        }
                    },
                    "night": {
                        "weather": {
                            "info": "多云",
                            "img": "1",
                            "temperature": "-3"
                        },
                        "wind": {
                            "direct": "北风",
                            "power": "微风"
                        }
                    }
                },
             	...
             	...
                {
                    "date": "2024-03-04",
                    "pt": "2024-02-27 12:00",
                    "day": {
                        "weather": {
                            "info": "阴",
                            "img": "2",
                            "temperature": "7"
                        },
                        "wind": {
                            "direct": "东北风",
                            "power": "微风"
                        }
                    },
                    "night": {
                        "weather": {
                            "info": "多云",
                            "img": "1",
                            "temperature": "-2"
                        },
                        "wind": {
                            "direct": "北风",
                            "power": "3~4级"
                        }
                    }
                }
            ]
        },
        "air": {
            "forecasttime": "2024-02-27 15:00",
            "aqi": 81,
            "aq": 2,
            "text": "良",
            "aqiCode": "99006;99008;99009;99010;99011;99013;99014;99015;99016;99017"
        },
        "tempchart": [
            {
                "time": "2024/02/20",
                "max_temp": -1.3,
                "min_temp": -5.6,
                "day_img": "9999",
                "day_text": "9999",
                "night_img": "9999",
                "night_text": "9999"
            },
          	...
          	...
            {
                "time": "2024/03/04",
                "max_temp": 7,
                "min_temp": -2,
                "day_img": "2",
                "day_text": "阴",
                "night_img": "1",
                "night_text": "多云"
            }
        ],
        "passedchart": [
            {
                "rain1h": 0,
                "rain24h": 9999,
                "rain12h": 9999,
                "rain6h": 9999,
                "temperature": 5.5,
                "tempDiff": "",
                "humidity": 38,
                "pressure": 1022,
                "windDirection": 262,
                "windSpeed": 3.3,
                "time": "2024-02-27 16:00"
            },
           	...
           	...
            {
                "rain1h": 0,
                "rain24h": 9999,
                "rain12h": 9999,
                "rain6h": 9999,
                "temperature": 3.9,
                "tempDiff": "",
                "humidity": 36,
                "pressure": 1029,
                "windDirection": 231,
                "windSpeed": 3.3,
                "time": "2024-02-26 17:00"
            }
        ],
        "climate": {
            "time": "1981年-2010年",
            "month": [
                {
                    "month": 1,
                    "maxTemp": 1.8,
                    "minTemp": -7.3,
                    "precipitation": 2.8
                },
           		....
           		....
                {
                    "month": 11,
                    "maxTemp": 10.2,
                    "minTemp": 0.6,
                    "precipitation": 9.6
                },
                {
                    "month": 12,
                    "maxTemp": 3.5,
                    "minTemp": -5.1,
                    "precipitation": 2
                }
            ]
        },
        "radar": {
            "title": "华北",
            "image": "/product/2024/02/27/RDCP/SEVP_AOC_RDCP_SLDAS3_ECREF_ANCN_L88_PI_20240227082400000.PNG?v=1709022833859",
            "url": "/publish/radar/huabei.html"
        }
    }
}

注意:过期的数据值都为9999。

使用 Qt 代码提取数据

看公众号…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fu_Lin_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值