中国国家气象局api数据分析
天气预报是程序中很常见的功能 然而现在免费好用的天气api却很难找 不妨仔细的看一下国家气象局 本分就此网站来获取天气
地址及页面分析
地址分析
通过尝试,想要获取某个城市或者地址的天气页面有两种方式,一种是通过经纬度访问中国天气网,另一种是通过城市id访问中国天气网 两个地址都能得到北京市的天气情况 不过前一种是经纬度,另一种是城市id.
关于城市的id可以查看中国天气网可以得到全国2500多个城市县区及全球其他国家6000多个城市的数据 可以直接通过以上的接口获得相关城市的天气数据
页面分析
id查询
我们先访问以下中国天气网页面 看看整个页面都请求了什么
中国天气网 这个页面并没有返回对应的数据 也是 现在谁家的网页还能都是静态的 然后是这个位置的素质五连
后面是一个时间戳 模仿了对应的请求格式 但是每次访问的时候都被拒绝应该是页面js代码中有控制 看来直接通过城市id获取不行了
经纬度查询
如果直接通过经纬度地址进行访问中国天气网通过分析可以在直接访问的地址中找到返回这个部分数据
数据分析
([{"TEMMIN":19,"reftime":"201709022000","WIND1":"04","WINS1":"01","WIND2":"01","WINS2":"00","WEATHER1":"02","WEATHER2":"02","TEMMAX":29},{"TEMMIN":19,"reftime":"201709032000","WIND1":"01","WINS1":"00","WIND2":"08","WINS2":"00","WEATHER1":"02","WEATHER2":"02","TEMMAX":27},{"TEMMIN":18,"reftime":"201709042000","WIND1":"01","WINS1":"00","WIND2":"01","WINS2":"00","WEATHER1":"01","WEATHER2":"02","TEMMAX":27},{"TEMMIN":19,"reftime":"201709052000","WIND1":"01","WINS1":"00","WIND2":"08","WINS2":"00","WEATHER1":"07","WEATHER2":"02","TEMMAX":29},{"TEMMIN":18,"reftime":"201709062000","WIND1":"01","WINS1":"00","WIND2":"02","WINS2":"00","WEATHER1":"00","WEATHER2":"00","TEMMAX":30}],[{"WIND":"05","WEA08":"02","reftime":"201709010800","PRE08":"0.0","TMAX":28,"TMIN":21,"WEA20":"02","PRE20":"0.0","WINS":"01"}])
复制代码
这部分的数据可以看出来是个json格式数据 取其中一部分来分析
{"TEMMIN":19,"reftime":"201709022000","WIND1":"04","WINS1":"01","WIND2":"01","WINS2":"00","WEATHER1":"02","WEATHER2":"02","TEMMAX":29}
复制代码
经过整理后发现对应数据如下
TEMMIN : 最低温度 TEMMAX : 最高温度 reftime : 时间 WEATHER1 : 天气1 (晴转多云 的晴) WEATHER2 : 天气2 (晴转多云 的多云)
weather的对应如下
{"00":"晴","01":"多云","02":"阴","03":"阵雨","04":"雷阵雨","05":"雷阵雨伴有冰雹","06":"雨夹雪","07":"小雨","08":"中雨","09":"大雨","10":"暴雨","11":"大暴雨","12":"特大暴雨","13":"阵雪","14":"小雪","15":"中雪","16":"大雪","17":"暴雪","18":"雾","19":"冻雨","20":"沙尘暴","21":"小到中雨","22":"中到大雨","23":"大到暴雨","24":"暴雨到大暴雨","25":"大暴雨到特大暴雨","26":"小到中雪","27":"中到大雪","28":"大到暴雪","29":"浮尘","30":"扬沙","31":"强沙尘暴","53":"霾","99":""}
复制代码
WIND1 : 风向1 WINS1 : 风向1等级 WIND2 : 风向2 WINS2 : 风向2等级
WIND对应如下
{"0":"无持续风向","1":"东北风","2":"东风","3":"东南风","4":"南风","5":"西南风","6":"西风","7":"西北风","8":"北风","9":"旋转风"}
复制代码
WINS对应如下:
{"0":"微风","1":"3-4级","2":"4-5级","3":"5-6级","4":"6-7级","5":"7-8级","6":"8-9级","7":"9-10级","8":"10-11级","9":"11-12级"};
复制代码
获取流程
通过经纬度可以直接获取天气信息 而且精度较高可以定位当前位置后再获取天气信息 但是只适用国内的天气信息 可以本地缓存全国所有地区的经纬度或者通过城市id访问天气信息的页面中获取经纬度 看实际使用情况斟酌
而通过城市id的方法可以获得全球的天气信息
最后
页面还有很多数据可以发现~