前言
在一些嵌入式设备中,有时候我们需要获取天气信息,但是我们本身没有类似服务,如果直接访问天气网站网页,解析数据会很头疼,因此最好的办法是访问公用的api接口。
一、api接口
我们使用的是中央气象台的api接口:
http://www.nmc.cn/rest/province #省份获取
http://www.nmc.cn/rest/province/{省份代码} #城市获取
http://www.nmc.cn/rest/weather?stationid={城市代码} #城市天气预报获取
二、接口调用
1.查询省份代码
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": "ASX",
"name": "山西省",
"url": "/publish/forecast/ASX.html"
},
{
"code": "ANM",
"name": "内蒙古自治区",
"url": "/publish/forecast/ANM.html"
},
{
"code": "ALN",
"name": "辽宁省",
"url": "/publish/forecast/ALN.html"
},
{
"code": "AJL",
"name": "吉林省",
"url": "/publish/forecast/AJL.html"
},
{
"code": "AHL",
"name": "黑龙江省",
"url": "/publish/forecast/AHL.html"
},
{
"code": "ASH",
"name": "上海市",
"url": "/publish/forecast/ASH.html"
},
{
"code": "AJS",
"name": "江苏省",
"url": "/publish/forecast/AJS.html"
},
{
"code": "AZJ",
"name": "浙江省",
"url": "/publish/forecast/AZJ.html"
},
{
"code": "AAH",
"name": "安徽省",
"url": "/publish/forecast/AAH.html"
},
{
"code": "AFJ",
"name": "福建省",
"url": "/publish/forecast/AFJ.html"
},
{
"code": "AJX",
"name": "江西省",
"url": "/publish/forecast/AJX.html"
},
{
"code": "ASD",
"name": "山东省",
"url": "/publish/forecast/ASD.html"
},
{
"code": "AHA",
"name": "河南省",
"url": "/publish/forecast/AHA.html"
},
{
"code": "AHB",
"name": "湖北省",
"url": "/publish/forecast/AHB.html"
},
{
"code": "AHN",
"name": "湖南省",
"url": "/publish/forecast/AHN.html"
},
{
"code": "AGD",
"name": "广东省",
"url": "/publish/forecast/AGD.html"
},
{
"code": "AGX",
"name": "广西壮族自治区",
"url": "/publish/forecast/AGX.html"
},
{
"code": "AHI",
"name": "海南省",
"url": "/publish/forecast/AHI.html"
},
{
"code": "ACQ",
"name": "重庆市",
"url": "/publish/forecast/ACQ.html"
},
{
"code": "ASC",
"name": "四川省",
"url": "/publish/forecast/ASC.html"
},
{
"code": "AGZ",
"name": "贵州省",
"url": "/publish/forecast/AGZ.html"
},
{
"code": "AYN",
"name": "云南省",
"url": "/publish/forecast/AYN.html"
},
{
"code": "AXZ",
"name": "西藏自治区",
"url": "/publish/forecast/AXZ.html"
},
{
"code": "ASN",
"name": "陕西省",
"url": "/publish/forecast/ASN.html"
},
{
"code": "AGS",
"name": "甘肃省",
"url": "/publish/forecast/AGS.html"
},
{
"code": "AQH",
"name": "青海省",
"url": "/publish/forecast/AQH.html"
},
{
"code": "ANX",
"name": "宁夏回族自治区",
"url": "/publish/forecast/ANX.html"
},
{
"code": "AXJ",
"name": "新疆维吾尔自治区",
"url": "/publish/forecast/AXJ.html"
},
{
"code": "AXG",
"name": "香港特别行政区",
"url": "/publish/forecast/AXG.html"
},
{
"code": "AAM",
"name": "澳门特别行政区",
"url": "/publish/forecast/AAM.html"
},
{
"code": "ATW",
"name": "台湾省",
"url": "/publish/forecast/ATW.html"
}
]
观察可发现,省份都是A+省份前两字的首字母,除河南(HA)、海南(HI)、陕西(SN)外;因为表格不大,可以直接保存下来,方便直接查询城市信息;
2.查询城市代码
以北京为例:
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": "54433",
"province": "北京市",
"city": "朝阳",
"url": "/publish/forecast/ABJ/chaoyang.html"
},
{
"code": "54594",
"province": "北京市",
"city": "大兴",
"url": "/publish/forecast/ABJ/daxing.html"
},
{
"code": "54596",
"province": "北京市",
"city": "房山",
"url": "/publish/forecast/ABJ/fangshan.html"
},
{
"code": "54514",
"province": "北京市",
"city": "丰台",
"url": "/publish/forecast/ABJ/fengtai.html"
},
{
"code": "54399",
"province": "北京市",
"city": "海淀",
"url": "/publish/forecast/ABJ/haidian.html"
},
{
"code": "54419",
"province": "北京市",
"city": "怀柔",
"url": "/publish/forecast/ABJ/huairou.html"
},
{
"code": "54505",
"province": "北京市",
"city": "门头沟",
"url": "/publish/forecast/ABJ/mentougou.html"
},
{
"code": "54416",
"province": "北京市",
"city": "密云",
"url": "/publish/forecast/ABJ/miyun.html"
},
{
"code": "54424",
"province": "北京市",
"city": "平谷",
"url": "/publish/forecast/ABJ/pinggu.html"
},
{
"code": "54513",
"province": "北京市",
"city": "石景山",
"url": "/publish/forecast/ABJ/shijingshan.html"
},
{
"code": "54398",
"province": "北京市",
"city": "顺义",
"url": "/publish/forecast/ABJ/shunyi.html"
},
{
"code": "54431",
"province": "北京市",
"city": "通州",
"url": "/publish/forecast/ABJ/tongzhou.html"
},
{
"code": "54406",
"province": "北京市",
"city": "延庆",
"url": "/publish/forecast/ABJ/yanqing.html"
}
]
城市信息和城市名称就没什么对应关系了,但是对应关系是确定的因此如果可以推荐保存下来所有省份的关系表,方便设备直接查询天气;
3.查询城市天气
以朝阳区为例:
http://www.nmc.cn/rest/weather?stationid=54433
{
"msg": "success",
"code": 0,
"data": {
"real": {
"station": {
"code": "54433",
"province": "北京市",
"city": "朝阳",
"url": "/publish/forecast/ABJ/chaoyang.html"
},
"publish_time": "2021-02-04 10:10",
"weather": {
"temperature": 2.9,
"temperatureDiff": 4.8,
"airpressure": 1021.0,
"humidity": 24.0,
"rain": 0.0,
"rcomfort": 40,
"icomfort": -2,
"info": "晴",
"img": "0",
"feelst": -1.1
},
"wind": {
"direct": "西南风",
"power": "微风",
"speed": ""
},
"warn": {
"alert": "9999",
"pic": "9999",
"province": "9999",
"city": "9999",
"url": "9999",
"issuecontent": "9999",
"fmeans": "9999",
"signaltype": "9999",
"signallevel": "9999",
"pic2": "9999"
}
},
"predict": {
"station": {
"code": "54433",
"province": "北京市",
"city": "朝阳",
"url": "/publish/forecast/ABJ/chaoyang.html"
},
"publish_time": "2021-02-04 08:00",
"detail": [
{
"date": "2021-02-04",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "7"
},
"wind": {
"direct": "西南风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "-4"
},
"wind": {
"direct": "西南风",
"power": "微风"
}
}
},
{
"date": "2021-02-05",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "13"
},
"wind": {
"direct": "西北风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "0"
},
"wind": {
"direct": "西北风",
"power": "微风"
}
}
},
{
"date": "2021-02-06",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "12"
},
"wind": {
"direct": "西北风",
"power": "3~4级"
}
},
"night": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "-2"
},
"wind": {
"direct": "南风",
"power": "微风"
}
}
},
{
"date": "2021-02-07",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "5"
},
"wind": {
"direct": "东北风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "-4"
},
"wind": {
"direct": "南风",
"power": "微风"
}
}
},
{
"date": "2021-02-08",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "7"
},
"wind": {
"direct": "西南风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "-4"
},
"wind": {
"direct": "西北风",
"power": "微风"
}
}
},
{
"date": "2021-02-09",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "10"
},
"wind": {
"direct": "西南风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "晴",
"img": "0",
"temperature": "-2"
},
"wind": {
"direct": "东北风",
"power": "微风"
}
}
},
{
"date": "2021-02-10",
"pt": "2021-02-04 08:00",
"day": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "8"
},
"wind": {
"direct": "东风",
"power": "微风"
}
},
"night": {
"weather": {
"info": "多云",
"img": "1",
"temperature": "0"
},
"wind": {
"direct": "东北风",
"power": "微风"
}
}
}
]
},
"air": "",
"tempchart": [
{
"time": "2021/01/28",
"max_temp": 0.5,
"min_temp": -5.6,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/01/29",
"max_temp": 0.8,
"min_temp": -9.4,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/01/30",
"max_temp": 6.9,
"min_temp": -7.5,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/01/31",
"max_temp": 0.3,
"min_temp": -5.6,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/02/01",
"max_temp": 3.9,
"min_temp": -4.1,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/02/02",
"max_temp": 1.8,
"min_temp": -7.7,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/02/03",
"max_temp": 6.7,
"min_temp": -7.3,
"day_img": "9999",
"day_text": "9999",
"night_img": "9999",
"night_text": "9999"
},
{
"time": "2021/02/04",
"max_temp": 7.0,
"min_temp": -4.0,
"day_img": "0",
"day_text": "晴",
"night_img": "1",
"night_text": "多云"
},
{
"time": "2021/02/05",
"max_temp": 13.0,
"min_temp": 0.0,
"day_img": "0",
"day_text": "晴",
"night_img": "0",
"night_text": "晴"
},
{
"time": "2021/02/06",
"max_temp": 12.0,
"min_temp": -2.0,
"day_img": "0",
"day_text": "晴",
"night_img": "1",
"night_text": "多云"
},
{
"time": "2021/02/07",
"max_temp": 5.0,
"min_temp": -4.0,
"day_img": "0",
"day_text": "晴",
"night_img": "1",
"night_text": "多云"
},
{
"time": "2021/02/08",
"max_temp": 7.0,
"min_temp": -4.0,
"day_img": "1",
"day_text": "多云",
"night_img": "0",
"night_text": "晴"
},
{
"time": "2021/02/09",
"max_temp": 10.0,
"min_temp": -2.0,
"day_img": "0",
"day_text": "晴",
"night_img": "0",
"night_text": "晴"
},
{
"time": "2021/02/10",
"max_temp": 8.0,
"min_temp": 0.0,
"day_img": "1",
"day_text": "多云",
"night_img": "1",
"night_text": "多云"
}
],
"passedchart": [
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 2.7,
"tempDiff": "",
"humidity": 24.0,
"pressure": 1021.0,
"windDirection": 230.0,
"windSpeed": 5.6,
"time": "2021-02-04 10:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 1.3,
"tempDiff": "",
"humidity": 27.0,
"pressure": 1022.0,
"windDirection": 258.0,
"windSpeed": 3.9,
"time": "2021-02-04 09:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -0.2,
"tempDiff": "",
"humidity": 29.0,
"pressure": 1022.0,
"windDirection": 264.0,
"windSpeed": 1.2,
"time": "2021-02-04 08:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 0.8,
"tempDiff": "",
"humidity": 26.0,
"pressure": 1022.0,
"windDirection": 249.0,
"windSpeed": 3.0,
"time": "2021-02-04 07:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 1.2,
"tempDiff": "",
"humidity": 25.0,
"pressure": 1022.0,
"windDirection": 255.0,
"windSpeed": 6.5,
"time": "2021-02-04 06:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 1.3,
"tempDiff": "",
"humidity": 24.0,
"pressure": 1022.0,
"windDirection": 241.0,
"windSpeed": 5.2,
"time": "2021-02-04 05:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 0.8,
"tempDiff": "",
"humidity": 24.0,
"pressure": 1022.0,
"windDirection": 224.0,
"windSpeed": 3.9,
"time": "2021-02-04 04:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 0.8,
"tempDiff": "",
"humidity": 24.0,
"pressure": 1023.0,
"windDirection": 224.0,
"windSpeed": 6.3,
"time": "2021-02-04 03:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -0.1,
"tempDiff": "",
"humidity": 29.0,
"pressure": 1023.0,
"windDirection": 255.0,
"windSpeed": 3.1,
"time": "2021-02-04 02:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -3.2,
"tempDiff": "",
"humidity": 36.0,
"pressure": 1023.0,
"windDirection": 111.0,
"windSpeed": 0.8,
"time": "2021-02-04 01:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -2.2,
"tempDiff": "",
"humidity": 32.0,
"pressure": 1023.0,
"windDirection": 119.0,
"windSpeed": 0.6,
"time": "2021-02-04 00:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -1.8,
"tempDiff": "",
"humidity": 30.0,
"pressure": 1023.0,
"windDirection": 116.0,
"windSpeed": 1.5,
"time": "2021-02-03 23:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -0.9,
"tempDiff": "",
"humidity": 27.0,
"pressure": 1023.0,
"windDirection": 62.0,
"windSpeed": 1.3,
"time": "2021-02-03 22:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 3.6,
"tempDiff": "",
"humidity": 20.0,
"pressure": 1023.0,
"windDirection": 312.0,
"windSpeed": 1.2,
"time": "2021-02-03 21:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 3.2,
"tempDiff": "",
"humidity": 19.0,
"pressure": 1023.0,
"windDirection": 303.0,
"windSpeed": 2.4,
"time": "2021-02-03 20:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 4.3,
"tempDiff": "",
"humidity": 17.0,
"pressure": 1022.0,
"windDirection": 298.0,
"windSpeed": 6.8,
"time": "2021-02-03 19:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 4.7,
"tempDiff": "",
"humidity": 16.0,
"pressure": 1022.0,
"windDirection": 303.0,
"windSpeed": 8.2,
"time": "2021-02-03 18:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 5.5,
"tempDiff": "",
"humidity": 15.0,
"pressure": 1021.0,
"windDirection": 312.0,
"windSpeed": 8.9,
"time": "2021-02-03 17:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 6.3,
"tempDiff": "",
"humidity": 14.0,
"pressure": 1019.0,
"windDirection": 303.0,
"windSpeed": 10.2,
"time": "2021-02-03 16:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 6.7,
"tempDiff": "",
"humidity": 14.0,
"pressure": 1018.0,
"windDirection": 266.0,
"windSpeed": 9.3,
"time": "2021-02-03 15:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 6.6,
"tempDiff": "",
"humidity": 14.0,
"pressure": 1017.0,
"windDirection": 258.0,
"windSpeed": 10.1,
"time": "2021-02-03 14:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 4.8,
"tempDiff": "",
"humidity": 19.0,
"pressure": 1017.0,
"windDirection": 215.0,
"windSpeed": 1.6,
"time": "2021-02-03 13:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": 1.8,
"tempDiff": "",
"humidity": 26.0,
"pressure": 1018.0,
"windDirection": 309.0,
"windSpeed": 1.5,
"time": "2021-02-03 12:00"
},
{
"rain1h": 0.0,
"rain24h": 9999.0,
"rain12h": 9999.0,
"rain6h": 9999.0,
"temperature": -0.1,
"tempDiff": "",
"humidity": 28.0,
"pressure": 1019.0,
"windDirection": 43.0,
"windSpeed": 1.5,
"time": "2021-02-03 11:00"
}
],
"climate": {
"time": "1981年-2010年",
"month": [
{
"month": 1,
"maxTemp": 2.0,
"minTemp": -8.4,
"precipitation": 2.7
},
{
"month": 2,
"maxTemp": 5.8,
"minTemp": -5.5,
"precipitation": 3.8
},
{
"month": 3,
"maxTemp": 12.3,
"minTemp": 0.4,
"precipitation": 9.4
},
{
"month": 4,
"maxTemp": 20.7,
"minTemp": 7.7,
"precipitation": 22.9
},
{
"month": 5,
"maxTemp": 26.6,
"minTemp": 13.6,
"precipitation": 36.8
},
{
"month": 6,
"maxTemp": 30.3,
"minTemp": 18.6,
"precipitation": 76.1
},
{
"month": 7,
"maxTemp": 31.1,
"minTemp": 21.8,
"precipitation": 164.6
},
{
"month": 8,
"maxTemp": 30.0,
"minTemp": 20.7,
"precipitation": 156.9
},
{
"month": 9,
"maxTemp": 26.1,
"minTemp": 14.8,
"precipitation": 50.5
},
{
"month": 10,
"maxTemp": 19.4,
"minTemp": 7.3,
"precipitation": 24.4
},
{
"month": 11,
"maxTemp": 10.3,
"minTemp": -0.7,
"precipitation": 9.4
},
{
"month": 12,
"maxTemp": 3.8,
"minTemp": -6.0,
"precipitation": 1.7
}
]
},
"radar": {
"title": "华北",
"image": "/product/2021/02/04/RDCP/SEVP_AOC_RDCP_SLDAS_EBREF_ANCN_L88_PI_20210204022400001.PNG?v=1612406126602",
"url": "/publish/radar/huabei.html"
}
}
}
关于数据的含义,推荐对照root->data->real->station->url网页信息,其中9999.0表示要素缺测,要素如果无观测为9998。
//TODO C代码获取数据的解析示例
总结
获取天气信息有很多公用接口,上面的内容只是以中央气象台接口作为示例,不同的接口根据接口情况自己解析即可。
参考文章
- 气象科学数据中心 信息更全面,而且还有各种历史信息,不过接口限制次数;
- 从天气预报网页API获取天气预报数据
- 开源天气预报api整理