网页天气预报
国家气象局提供的天气预报接口
接口地址:
1 http://www.weather.com.cn/data/sk/101010100.html 2 3 http://www.weather.com.cn/data/cityinfo/101010100.html 4 5 http://m.weather.com.cn/data/101010100.html
第三个接口,返回信息比较全面,下面使用第三个接口实例。
返回值中图片链接我使用http://www.36wu.com/WeatherPic.aspx中的图标,天气图标名称与返回数据大致相符!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>weather</title> <script type="text/javascript" src="jquery-1.4.1.js"></script> </head> <body> </body> </html> <script type="text/javascript"> var _city="";//城市 var _temp="";//温度 var _weather="";//天气状态 var _weather_img=""; var weatherUrl="http://m.weather.com.cn/data/101190801.html"; //此处101190801为 江苏徐州城市 代码,文章下面有具体城市信息。 $.ajax({ type: 'GET', url: weatherUrl, dataType: "json", success: function(html) { if(html) { _city= html["weatherinfo"]["city"]; _temp=html["weatherinfo"]["temp1"]; _weather=html["weatherinfo"]["weather1"]; _weather_img=getWeatherImg(html["weatherinfo"]["img1"]); } else { //网络异常/数据获取出错后显示默认配置天气 _city= "徐州"; _temp="24℃~17℃"; _weather="晴天"; _weather_img=getWeatherImg(0); } } }); document.write("<span style='font-size:13px;'>"+_city +_weather_img+ _temp+"<span style='margin-left:5px;'></span>"+_weather+"</span>"); function getWeatherImg(id) { var number =""; if(id < 10) number ="0"+id; else {number=""+id; if(id=="99") number="02";}//99暂时没懂是什么图标,不处理。研究明白的亲,请指教! return "<img src='../images/weather_img/"+number+".png' title='"+_weather+"' style='margin:0px 5px 0px 5px; width:16px; height:16px;' />"; } </script>
返回的json数据格式,解释
{ "weatherinfo":{ <!-- 基本信息 --> "city":"福州", "city_en":"fuzhou", "date_y":"2012年5月14日", "date":"", "week":"星期一", "fchh":"08", "cityid":"101230101", <!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 --> "temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃", <!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 --> "tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉", <!-- 天气描述 --> "weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨", <!-- 天气描述图片序号 --> "img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3", <!-- 图片名称 --> "img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"阵雨", <!-- 风速描述 --> "wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风", <!-- 风速级别描述 --> "fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级", <!-- 今天穿衣指数 --> "index":"热", "index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。", <!-- 48小时穿衣指数 --> "index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。", <!-- 紫外线及48小时紫外线 --> "index_uv":"弱","index48_uv":"最弱", <!-- 洗车 --> "index_xc":"不宜", <!-- 旅游 --> "index_tr":"适宜",、 <!-- 舒适指数 --> "index_co":"较不舒适", "st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18", <!-- 晨练 --> "index_cl":"较不宜", <!-- 晾晒 --> "index_ls":"不太适宜", <!-- 过敏 --> "index_ag":"不易发" } }
中国城市代码:
直辖市
"北京","上海","天津","重庆"
"101010100","101020100","101030100","101040100"
特别行政区
"香港","澳门"
"101320101","101330101"
黑龙江
"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"
"101050101","101050201","101050301","101050901","101050801","101051301","101051201","101051101","101050401","101051002","101050601","101050501","101050701"
吉林
"长春","延吉","吉林","白山","白城","四平","松原","辽源","大安","通化"
"101060101","101060301","101060201","101060901","101060601","101060401","101060801","101060701","101060603","101060501"
辽宁
"沈阳","大连","葫芦岛","盘锦","本溪","抚顺","铁岭","辽阳","营口","阜新","朝阳","锦州","丹东","鞍山"
"101070101","101070201","101071401","101071301","101070501","101070401","101071101","101071001","101070801","101070901","101071201","101070701","101070601","101070301"
内蒙古
"呼和浩特","呼伦贝尔","锡林浩特","包头","赤峰","海拉尔","乌海","鄂尔多斯","通辽"
"101080101","101081000","101080901","101080201","101080601","101081001","101080301","101080701","101080501"
河北
"石家庄","唐山","张家口","廊坊","邢台","邯郸","沧州","衡水","承德","保定","秦皇岛"
"101090101","101090501","101090301","101090601","101090901","101091001","101090701","101090801","101090402","101090201","101091101"
河南
"郑州","开封","洛阳","平顶山","焦作","鹤壁","新乡","安阳","濮阳","许昌","漯河","三门峡","南阳","商丘","信阳","周口","驻马店"
"101180101","101180801","101180901","101180501","101181101","101181201","101180301","101180201","101181301","101180401","101181501","101181701","101180701","101181001","101180601","101181401","101181601"
山东
"济南","青岛","淄博","威海","曲阜","临沂","烟台","枣庄","聊城","济宁","菏泽","泰安","日照","东营","德州","滨州","莱芜","潍坊"
"101120101","101120201","101120301","101121301","101120710","101120901","101120501","101121401","101121701","101120701","101121001","101120801","101121501","101121201","101120401","101121101","101121601","101120601"
山西
"太原","阳泉","晋城","晋中","临汾","运城","长治","朔州","忻州","大同","吕梁"
"101100101","101100301","101100601","101100401","101100701","101100801","101100501","101100901","101101001","101100201","101101101"
江苏
"南京","苏州","昆山","南通","太仓","吴县","徐州","宜兴","镇江","淮安","常熟","盐城","泰州","无锡","连云港","扬州","常州","宿迁"
"101190101","101190401","101190404","101190501","101190408","101190406","101190801","101190203","101190301","101190901","101190402","101190701","101191201","101190201","101191001","101190601","101191101","101191301"
安徽
"合肥","巢湖","蚌埠","安庆","六安","滁州","马鞍山","阜阳","宣城","铜陵","淮北","芜湖","毫州","宿州","淮南","池州"
"101220101","101221601","101220201","101220601","101221501","101221101","101220501","101220801","101221401","101221301","101221201","101220301","101220901","101220701","101220401","101221701"
陕西
"西安","韩城","安康","汉中","宝鸡","咸阳","榆林","渭南","商洛","铜川","延安"
"101110101","101110510","101110701","101110801","101110901","101110200","101110401","101110501","101110601","101111001","101110300"
宁夏
"银川","固原","中卫","石嘴山","吴忠"
"101170101","101170401","101170501","101170201","101170301"
甘肃
"兰州","白银","庆阳","酒泉","天水","武威","张掖","甘南","临夏","平凉","定西","金昌"
"101160101","101161301","101160401","101160801","101160901","101160501","101160701","101050204","101161101","101160301","101160201","101160601"
青海
"西宁","海北","海西","黄南","果洛","玉树","海东","海南"
"101150101","101150801","101150701","101150301","101150501","101150601","101150201","101150401"
湖北
"武汉","宜昌","黄冈","恩施","荆州","神农架","十堰","咸宁","襄阳","孝感","随州","黄石","荆门","鄂州"
"101200101","101200901","101200501","101201001","101200801","101201201","101201101","101200701","101200201","101200401","101201301","101200601","101201401","101200301"
湖南
"长沙","邵阳","常德","郴州","吉首","株洲","娄底","湘潭","益阳","永州","岳阳","衡阳","怀化","韶山","张家界"
"101250101","101250901","101250601","101250501","101251501","101250301","101250801","101250201","101250701","101251401","101251001","101250401","101251201","101250202","101251101"
浙江
"杭州","湖州","金华","宁波","丽水","绍兴","衢州","嘉兴","台州","舟山","温州"
"101210101","101210201","101210901","101210401","101210801","101210501","101211001","101210301","101210601","101211101","101210701"
江西
"南昌","萍乡","九江","上饶","抚州","吉安","鹰潭","宜春","新余","景德镇","赣州"
"101240101","101240901","101240201","101240301","101240401","101240601","101241101","101240501","101241001","101240801","101240701"
福建
"福州","厦门","龙岩","南平","宁德","莆田","泉州","三明","漳州"
"101230101","101230201","101230701","101230901","101230301","101230401","101230501","101230801","101230601"
贵州
"贵阳","安顺","赤水","遵义","铜仁","六盘水","毕节","凯里","都匀"
"101260101","101260301","101260208","101260201","101260601","101260801","101260701","101260501","101260401"
四川
"成都","泸州","内江","凉山","阿坝","巴中","广元","乐山","绵阳","德阳","攀枝花","雅安","宜宾","自贡","甘孜州","达州","资阳","广安","遂宁","眉山","南充"
"101270101","101271001","101271201","101271601","101271901","101270901","101272101","101271401","101270401","101272001","101270201","101271701","101271101","101270301","101271801","101270601","101271301","101270801","101270701","101271501","101270501"
广东
"广州","深圳","潮州","韶关","湛江","惠州","清远","东莞","江门","茂名","肇庆","汕尾","河源","揭阳","梅州","中山","德庆","阳江","云浮","珠海","汕头","佛山"
"101280101","101280601","101281501","101280201","101281001","101280301","101281301","101281601","101281101","101282001","101280901","101282101","101281201","101281901","101280401","101281701","101280905","101281801","101281401","101280701","101280501","101280800"
广西
"南宁","桂林","阳朔","柳州","梧州","玉林","桂平","贺州","钦州","贵港","防城港","百色","北海","河池","来宾","崇左"
"101300101","101300501","101300510","101300301","101300601","101300901","101300802","101300701","101301101","101300801","101301401","101301001","101301301","101301201","101300401","101300201"
云南
"昆明","保山","楚雄","德宏","红河","临沧","怒江","曲靖","思茅","文山","玉溪","昭通","丽江","大理"
"101290101","101290501","101290801","101291501","101290301","101291101","101291201","101290401","101290901","101290601","101290701","101291001","101291401","101290201"
海南
"海口","三亚","儋州","琼山","通什","文昌"
"101310101","101310201","101310205","101310102","101310222","101310212"
新疆
"乌鲁木齐","阿勒泰","阿克苏","昌吉","哈密","和田","喀什","克拉玛依","石河子","塔城","库尔勒","吐鲁番","伊宁"
"101130101","101131401","101130801","101130401","101131201","101131301","101130901","101130201","101130301","101131101","101130601","101130501","101131001"
西藏
"拉萨","阿里","昌都","那曲","日喀则","山南","林芝"
"101140101","101140701","101140501","101140601","101140201","101140301","101140401"
台湾
"台北","高雄"
"101340102","101340201"
下列内容为网上一些杂信息,我整理有能用的看看:
1. XML接口 http://flash.weather.com.cn/wmaps/xml/china.xml 这个是全国天气的根节点,列出所有的省,其中的pyName字段是各个省XML的文件名,比如北京的是beijing,那就意味着北京的XML地址为 http://flash.weather.com.cn/wmaps/xml/beijing.xml 一个省的天气,其中列出该省各个市的数据,北京就列出各个区。 tmp1是最低温低,tmp2是最高温度,url非常重要,我们一会儿再说。state1和state2是神马转神马,每个数代表一个天气现象。天气现象非常多,我本想全部分析出来,后来直接放弃了这个想法。因为我看到了一个城市的天气现象的编码是26...我现在知道的有0.晴 1.多云 2.阴 6.雨夹雪 7.小雨 8.中雨 13.阵雪 14.小雪 其中后来发现知道这个没用,这个数字的主要作用是检索图片的!!!
2. 图片接口 http://m.weather.com.cn/img/c0.gif http://m.weather.com.cn/img/b0.gif http://www.weather.com.cn/m/i/weatherpic/29x20/d0.gif http://www.weather.com.cn/m2/i/icon_weather/29x20/n00.gif 这个图就是天气现象0(晴)的图片,其他天气现象的图片依此类推。c打头的图片是20*20像素的,b打头的是50*46像素的,d打头的是反白的图标,29*20像素,n打头的是夜间反白图标,29*20像素,注意这里的文件名是两位数字! 也许还有更多的图标,等待大家发掘啦~
3. JSON接口 真没想到~居然有JSON接口~JSON在iPhone上分析起来要比XML简单很多 http://m.weather.com.cn/data/101010200.html 这个是北京的JSON数据,那个HTML的名字是根据上文XML中的url得到的。这个JSON中包含了实时数据、7天天气预报、气象指数等丰富的数据
获取省级代码:http://www.weather.com.cn/data/list3/city.xml?level=1
获取城市代码(比如安徽是22):http://www.weather.com.cn/data/list3/city22.xml?level=2
获取区域代码(比如安庆是2206):http://www.weather.com.cn/data/list3/city2206.xml?level=3
获取到安徽省安庆市望江县的代码是220607
然后去加上中国代码请求URL:http://m.weather.com.cn/data/101220607.html
就可以获取当地天气。
另外再给几个有用的探索得到的URL:
天气 FLASH实况:http://flash.weather.com.cn/sk2/101220607.xml
实况FLASH:http://flash.weather.com.cn/sk2/shikuang.swf?id=101220607
三级选择菜单(注明,这里有四个INPUT,其中有一个被隐藏了,可用FIREFOX改下源代码查看):http://www.weather.com.cn/static/custom/search3.htm
实时天气(很有用哦):http://www.weather.com.cn/data/sk/101220607.html