智慧天气应用编程接口开放平台:http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml
一:JSON数据接口
中国气象局提供了三个天气查询的API接口:
气象数据开放平台:http://openweather.weather.com.cn/Home/Package/index.html
【1】http://www.weather.com.cn/data/sk/101190101.html
【2】http://www.weather.com.cn/data/cityinfo/101190101.html
【3】http://m.weather.com.cn/data/101190101.html
以json格式返回数据,第一和第二个接口返回当天实时的天气数据,第三个返回未来五天天气情况。
其中地址里的加粗显示的数字表示城市的代码:101190101 代表南京(获取城市代码的方法下面会说明)
第三个接口的返回数据示例及说明如下:
- {
- weatherinfo: {
- city: "南京",
- city_en: "nanjing",
- date_y: "2013年9月8日",
- date: "",
- week: "星期日",
- fchh: "18",
- cityid: "101190101", //城市代码
- temp1: "22℃~30℃", //第一天(当天)最高喝最低温度(摄氏温度)
- temp2: "23℃~29℃", //第二天最高喝最低温度(摄氏温度)
- temp3: "22℃~28℃", //第三天最高喝最低温度(摄氏温度)
- temp4: "23℃~30℃", //第四天最高喝最低温度(摄氏温度)
- temp5: "24℃~28℃", //第五天最高喝最低温度(摄氏温度)
- temp6: "23℃~28℃", //第六天最高喝最低温度(摄氏温度)
- tempF1: "71.6℉~86℉", //第一天(当天)最高喝最低温度(华氏温度)
- tempF2: "73.4℉~84.2℉", //第二天最高喝最低温度(华氏温度)
- tempF3: "71.6℉~82.4℉", //第三天最高喝最低温度(华氏温度)
- tempF4: "73.4℉~86℉", //第四天最高喝最低温度(华氏温度)
- tempF5: "75.2℉~82.4℉", //第五天最高喝最低温度(华氏温度)
- tempF6: "73.4℉~82.4℉", //第六天最高喝最低温度(华氏温度)
- weather1: "多云转阴", //第一天(当天)天气
- weather2: "小雨", //第二天天气
- weather3: "小雨",
- weather4: "阴",
- weather5: "小雨转中雨",
- weather6: "中雨转阴",
- img1: "1", //第一天的天气图片代码1
- img2: "2", //第一天的天气图片代码2
- img3: "7", //第二天的天气图片代码1
- img4: "99", //第二天的天气图片代码2
- img5: "7",
- img6: "99",
- img7: "2",
- img8: "99",
- img9: "7",
- img10: "8",
- img11: "8",
- img12: "2",
- img_single: "2"
- img_title1: "多云", //第一天的天气图片1的标题
- img_title2: "阴", //第一天的天气图片2的标题
- img_title3: "小雨", //第二天的天气图片1的标题
- img_title4: "小雨", //第二天的天气图片2的标题
- img_title5: "小雨",
- img_title6: "小雨",
- img_title7: "阴",
- img_title8: "阴",
- img_title9: "小雨",
- img_title10: "中雨",
- img_title11: "中雨",
- img_title12: "阴",
- img_title_single: "阴",
- wind1: "东风3-4级", //第一天风向范围
- wind2: "东风3-4级", //第二天风向范围
- wind3: "北风转东北风3-4级",
- wind4: "东北风3-4级",
- wind5: "东北风转西风3-4级",
- wind6: "西南风转西风3-4级",
- fx1: "东风",
- fx2: "东风",
- fl1: "3-4级", //第一天风级范围
- fl2: "3-4级",
- fl3: "3-4级",
- fl4: "3-4级",
- fl5: "3-4级",
- fl6: "3-4级",
- index: "热", //二十四小时穿衣指数
- index_d: "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。", //建议
- index48: "热", //四十八小时穿衣指数
- index48_d: "天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。", //建议
- index_uv: "弱", //二十四小时紫外线
- index48_uv: "弱", //四十八小时紫外线
- index_xc: "不宜", //洗车
- index_tr: "适宜", //旅游
- index_co: "较舒适", //舒适度
- st1: "30",
- st2: "20",
- st3: "27",
- st4: "19",
- st5: "26",
- st6: "18",
- index_cl: "较适宜", //晨练
- index_ls: "不太适宜", //晾晒
- index_ag: "极易发" //过敏
- }
- }
获取城市代码的方法:
1.首先获取省份列表
http://www.weather.com.cn/data/city3jdata/china.html
这个链接以json格式返回一个包含省份代码和省份名称的数组
- {
- 10101: "北京",
- 10102: "上海",
- .... ,
- 10119: "江苏",
- }
2.通过上面获得的省份代码获取城市列表:
如江苏的城市代码为:10119,通过下面的链接可以获得江苏省的城市列表
http://www.weather.com.cn/data/city3jdata/provshi/10119.html
- {
- 10: "连云港",
- 11: "常州",
- ...
- 01: "南京",
- }
3.获取城市下的区域信息
通过上一步可获取城市代码 如:南京为 01
把01接到第一步获得的省份代码后 获得 1011901 代表江苏省南京市
通过下面的链接可获取江苏省南京市的区域信息,加粗部分为刚刚生成的代码
http://www.weather.com.cn/data/city3jdata/station/1011901.html
- {
- 01: "南京",
- 02: "溧水",
- 03: "高淳",
- 04: "江宁",
- 05: "六合",
- 06: "江浦",
- 07: "浦口"
- }
4.获取区域代码
把上面获得的区域代码如:01(代表南京市区) 接到第三步获得的江苏省南京市的代码 1011901
得到 101190101 代表江苏省南京市南京市区的代码。
这串代码就是我们需要的。
http://m.weather.com.cn/data/101190101.html
放到这个链接里我们就能获得江苏省南京市南京市区的天气信息啦!
下面提供一个我写好的天气查询工具的演示地址和城市代码对照表的sql文件下载地址。
二:XML数据接口
请求示例:http://flash.weather.com.cn/wmaps/xml/china.xml
可以将上述URL中的china换成shanghai、henan等省市区拼音名称,获取相应数据,数据格式示例如下:
<china dn="day"> <city quName="黑龙江" pyName="heilongjiang" cityname="哈尔滨" state1="0" state2="0" stateDetailed="晴" tem1="27" tem2="17" windState="南风转西南风小于3级"/> <city quName="吉林" pyName="jilin" cityname="长春" state1="0" state2="0" stateDetailed="晴" tem1="30" tem2="18" windState="西南风小于3级"/> <city quName="辽宁" pyName="liaoning" cityname="沈阳" state1="0" state2="0" stateDetailed="晴" tem1="30" tem2="17" windState="西风转东南风小于3级"/> <city quName="海南" pyName="hainan" cityname="海口" state1="9" state2="8" stateDetailed="大雨转中雨" tem1="29" tem2="25" windState="东北风转南风5-6级"/> <city quName="内蒙古" pyName="neimenggu" cityname="呼和浩特" state1="1" state2="0" stateDetailed="多云转晴" tem1="24" tem2="12" windState="东南风3-4级"/> <city quName="新疆" pyName="xinjiang" cityname="乌鲁木齐" state1="0" state2="0" stateDetailed="晴" tem1="24" tem2="15" windState="微风"/> <city quName="西藏" pyName="xizang" cityname="拉萨" state1="1" state2="1" stateDetailed="多云" tem1="21" tem2="10" windState="微风"/> <city quName="青海" pyName="qinghai" cityname="西宁" state1="1" state2="7" stateDetailed="多云转小雨" tem1="27" tem2="12" windState="微风"/> <city quName="宁夏" pyName="ningxia" cityname="银川" state1="0" state2="1" stateDetailed="晴转多云" tem1="29" tem2="18" windState="微风"/> <city quName="甘肃" pyName="gansu" cityname="兰州" state1="0" state2="1" stateDetailed="晴转多云" tem1="32" tem2="15" windState="微风"/> <city quName="河北" pyName="hebei" cityname="石家庄" state1="1" state2="2" stateDetailed="多云转阴" tem1="30" tem2="21" windState="北风小于3级"/> <city quName="河南" pyName="henan" cityname="郑州" state1="0" state2="0" stateDetailed="晴" tem1="30" tem2="19" windState="微风"/> <city quName="湖北" pyName="hubei" cityname="武汉" state1="1" state2="1" stateDetailed="多云" tem1="29" tem2="22" windState="微风"/> <city quName="湖南" pyName="hunan" cityname="长沙" state1="1" state2="1" stateDetailed="多云" tem1="31" tem2="24" windState="北风小于3级"/> <city quName="山东" pyName="shandong" cityname="济南" state1="1" state2="1" stateDetailed="多云" tem1="31" tem2="21" windState="南风小于3级"/> <city quName="江苏" pyName="jiangsu" cityname="南京" state1="3" state2="3" stateDetailed="阵雨" tem1="24" tem2="21" windState="东北风3-4级"/> <city quName="安徽" pyName="anhui" cityname="合肥" state1="7" state2="7" stateDetailed="小雨" tem1="25" tem2="21" windState="东北风小于3级"/> <city quName="山西" pyName="shanxi" cityname="太原" state1="3" state2="3" stateDetailed="阵雨" tem1="25" tem2="14" windState="微风"/> <city quName="陕西" pyName="sanxi" cityname="西安" state1="0" state2="1" stateDetailed="晴转多云" tem1="31" tem2="21" windState="东北风小于3级"/> <city quName="四川" pyName="sichuan" cityname="成都" state1="2" state2="2" stateDetailed="阴" tem1="28" tem2="20" windState="南风小于3级"/> <city quName="云南" pyName="yunnan" cityname="昆明" state1="3" state2="3" stateDetailed="阵雨" tem1="26" tem2="17" windState="微风"/> <city quName="贵州" pyName="guizhou" cityname="贵阳" state1="1" state2="1" stateDetailed="多云" tem1="28" tem2="20" windState="南风小于3级"/> <city quName="浙江" pyName="zhejiang" cityname="杭州" state1="4" state2="1" stateDetailed="雷阵雨转多云" tem1="31" tem2="23" windState="北风转东北风小于3级"/> <city quName="福建" pyName="fujian" cityname="福州" state1="1" state2="1" stateDetailed="多云" tem1="35" tem2="26" windState="微风"/> <city quName="江西" pyName="jiangxi" cityname="南昌" state1="2" state2="3" stateDetailed="阴转阵雨" tem1="31" tem2="25" windState="微风"/> <city quName="广东" pyName="guangdong" cityname="广州" state1="1" state2="1" stateDetailed="多云" tem1="33" tem2="25" windState="微风"/> <city quName="广西" pyName="guangxi" cityname="南宁" state1="9" state2="8" stateDetailed="大雨转中雨" tem1="32" tem2="24" windState="东南风小于3级"/> <city quName="北京" pyName="beijing" cityname="北京" state1="3" state2="4" stateDetailed="阵雨转雷阵雨" tem1="27" tem2="19" windState="微风"/> <city quName="天津" pyName="tianjin" cityname="天津" state1="4" state2="4" stateDetailed="雷阵雨" tem1="31" tem2="22" windState="东南风小于3级"/> <city quName="上海" pyName="shanghai" cityname="上海" state1="2" state2="3" stateDetailed="阴转阵雨" tem1="29" tem2="24" windState="东北风转西北风3-4级"/> <city quName="重庆" pyName="chongqing" cityname="重庆" state1="7" state2="7" stateDetailed="小雨" tem1="26" tem2="21" windState="微风"/> <city quName="香港" pyName="xianggang" cityname="香港" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="31" tem2="28" windState="东风3-4级"/> <city quName="澳门" pyName="aomen" cityname="澳门" state1="3" state2="1" stateDetailed="阵雨转多云" tem1="31" tem2="26" windState="东风3-4级转微风"/> <city quName="台湾" pyName="taiwan" cityname="台北" state1="1" state2="7" stateDetailed="多云转小雨" tem1="32" tem2="25" windState="微风"/> <city quName="西沙" pyName="xisha" cityname="西沙" state1="1" state2="1" stateDetailed="多云" tem1="31" tem2="26" windState="南风5-6级"/> <city quName="南沙" pyName="nanshadao" cityname="南沙" state1="23" state2="23" stateDetailed="大到暴雨" tem1="31" tem2="27" windState="西南风4-5级"/> <city quName="钓鱼岛" pyName="diaoyudao" cityname="钓鱼岛" state1="0" state2="7" stateDetailed="晴转小雨" tem1="28" tem2="26" windState="微风"/> </china>