从天气预报网页API获取天气预报数据

您所在的位置:网站首页 请给我报一下唐山天气预报 从天气预报网页API获取天气预报数据

从天气预报网页API获取天气预报数据

2024-07-13 16:29| 来源: 网络整理| 查看: 265

用Java代码获取中央气象局的天气预报数据

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

文章目录 用Java代码获取中央气象局的天气预报数据1 获取相关接口2 省份获取接口数据3 城市获取接口数据4 城市天气预报获取接口数据5 使用Java代码提取数据

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键的值为城市获取的参数,如四川的参数为:ASC

3 城市获取接口数据

接口:http://www.nmc.cn/rest/province/ASC【测试一下】

[ {"code":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"}, {"code":"56171","province":"四川省","city":"阿坝","url":"/publish/forecast/ASC/aba.html"}, ... ... {"code":"56498","province":"四川省","city":"筠连","url":"/publish/forecast/ASC/yunlian.html"} ]

注意:这里的城市包括各市的区县,比如上面展示的筠连就是四川省宜宾市的辖县,这里code键的值就是访问城市天气数据接口的参数。

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

接口:http://www.nmc.cn/rest/weather?stationid=56294【测试一下】

{ "msg":"success", "code":0, "data": { "real": { "station":{"code":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"}, "publish_time":"2020-07-26 22:25", "weather":{"temperature":24.9,"temperatureDiff":2.2,"airpressure":948.0,"humidity":81.0,"rain":0.0,"rcomfort":74,"icomfort":1,"info":"-","img":"9999","feelst":24.9},"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":"56294","province":"四川省","city":"成都","url":"/publish/forecast/ASC/chengdu.html"}, "publish_time":"2020-07-26 20:00", "detail": [ {"date":"2020-07-26","pt":"2020-07-26 20:00", "day":{"weather":{"info":"9999","img":"9999","temperature":"9999"},"wind":{"direct":"9999","power":"9999"}},"night":{"weather":{"info":"晴","img":"0","temperature":"22"},"wind":{"direct":"无持续风向","power":"微风"}}}, {"date":"2020-07-27","pt":"2020-07-26 20:00","day":{"weather":{"info":"晴","img":"0","temperature":"34"},"wind":{"direct":"无持续风向","power":"微风"}},"night":{"weather":{"info":"晴","img":"0","temperature":"23"},"wind":{"direct":"无持续风向","power":"微风"}}}, ... ... {"date":"2020-08-01","pt":"2020-07-26 20:00","day":{"weather":{"info":"阵雨","img":"3","temperature":"30"},"wind":{"direct":"无持续风向","power":"微风"}},"night":{"weather":{"info":"阴","img":"2","temperature":"23"},"wind":{"direct":"无持续风向","power":"微风"}}} ] }, "air":{"forecasttime":"2020-07-26 21:00","aqi":65,"aq":2,"text":"良","aqiCode":"99052;99054;99055"}, "tempchart": [ {"time":"2020/07/19","max_temp":34.8,"min_temp":20.0,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"}, {"time":"2020/07/20","max_temp":29.4,"min_temp":23.8,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"}, {"time":"2020/07/21","max_temp":25.8,"min_temp":21.3,"day_img":"9999","day_text":"9999","night_img":"9999","night_text":"9999"}, ... ... {"time":"2020/07/30","max_temp":26.7,"min_temp":22.5,"day_img":"10","day_text":"暴雨","night_img":"2","night_text":"阴"}, {"time":"2020/07/31","max_temp":29.0,"min_temp":22.8,"day_img":"7","day_text":"小雨","night_img":"2","night_text":"阴"}, {"time":"2020/08/01","max_temp":30.7,"min_temp":23.2,"day_img":"3","day_text":"阵雨","night_img":"2","night_text":"阴"} ], "passedchart": [ {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":25.4,"tempDiff":"","humidity":76.0,"pressure":948.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 22:00"}, {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":27.1,"tempDiff":"","humidity":70.0,"pressure":947.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 21:00"}, {"rain1h":0.0,"rain24h":9999.0,"rain12h":9999.0,"rain6h":9999.0,"temperature":30.5,"tempDiff":"","humidity":55.0,"pressure":947.0,"windDirection":0.0,"windSpeed":0.0,"time":"2020-07-26 20:00"}, ... ], "climate": { "time":"1981年-2010年", "month": [ {"month":1,"maxTemp":9.1,"minTemp":2.6,"precipitation":8.7}, {"month":2,"maxTemp":11.4,"minTemp":4.8,"precipitation":12.4}, ... {"month":12,"maxTemp":10.3,"minTemp":4.1,"precipitation":6.0} ] }, "radar":{"title":"成都","image":"/product/2020/07/26/RDCP/SEVP_AOC_RDCP_SLDAS_EBREF_AZ9280_L88_PI_20200726144200000.PNG?v=1595774905591","url":"/publish/radar/si-chuan/cheng-du.htm"} } }

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

5 使用Java代码提取数据

这里用四川省成都市预报数据的tempchart键为例。获取步骤如下:

使用URL类打开链接使用字节流读取数据使用字符流将读取字节流中的数据将字符流数据以字符的形式保存到StringBuilder字符构造类中使用fastjson将StringBuilder保存的字符数据转换成json对象使用fastjson中相关的方法提取需要的数据

Java代码:

public void getValue() { String url = "http://www.nmc.cn/rest/weather?stationid=56294"; StringBuilder sb = new StringBuilder();//获取拼接读取到的json字符 try { URL u = new URL(url); URLConnection con = u.openConnection(); //字节输入流读取json数据,并将编码格式设置为utf8 InputStreamReader in = new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8); //字符流读取输入流的数据 BufferedReader br = new BufferedReader(in); String inputLine;//循环中间量 while ((inputLine = br.readLine()) != null) { sb.append(inputLine); } br.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } //将字符串转换为json对象 JSONObject json = JSON.parseObject(sb.toString()); // System.out.println(json); //获取data键的值,值为json格式,所以可以转换为json对象 JSONObject data = json.getJSONObject("data"); // System.out.println(data); //data键中的tempchar键的值是一个数组,所以使用JSONArray对象接收值 JSONArray tempchart = data.getJSONArray("tempchart"); // System.out.println(tempchart); //一共14条数据,过去7天和未来7天,这里只取未来7天数据。 for (int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3