获取天气预报的整理

关于天气预报(谷歌,雅虎,中国气象局)的api,大家可以参考http://blog.csdn.net/fengyun1989/article/details/7341166

中国天气网(www.weather.com.cn)

通过访问http://m.weather.com.cn/data5/city.xml得到一级列表(省、直辖市、自治区)
通过访问http://m.weather.com.cn/data5/city + PROVINCE_ID +.xml得到(县级城市)列表
通过访问http://m.weather.com.cn/data5/city + CITY_ID +.xml得到城市天气预报信息ID

天气预报信息的请求地址是:http://m.weather.com.cn/data/+ WEATHER_ID +.html

{“weatherinfo”:{“city”:”北京”,”city_en”:”beijing”,”date_y”:”2011年5月8日”,”date”:”辛卯年”,”week”:”星期日”,”fchh”:”11″,”cityid”:”101010100″,”temp1″:”22℃~12℃”,”temp2″:”23℃~11℃”,”temp3″:”26℃~13℃”,”temp4″:”23℃~12℃”,”temp5″:”24℃~13℃”,”temp6″:”25℃~14℃”,”tempF1″:”71.6℉~53.6℉”,”tempF2″:”73.4℉~51.8℉”,”tempF3″:”78.8℉~55.4℉”,”tempF4″:”73.4℉~53.6℉”,”tempF5″:”75.2℉~55.4℉”,”tempF6″:”77℉~57.2℉”,”weather1″:”中雨转小雨”,”weather2″:”阴转晴”,”weather3″:”晴转阴”,”weather4″:”阵雨转晴”,”weather5″:”晴”,”weather6″:”晴”,”img1″:”8″,”img2″:”7″,”img3″:”2″,”img4″:”0″,”img5″:”0″,”img6″:”2″,”img7″:”3″,”img8″:”0″,”img9″:”0″,”img10″:”99″,”img11″:”0″,”img12″:”99″,”img_single”:”8″,”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″:”微风转北风小于3级”,”wind5″:”北风3-4级转微风”,”wind6″:”微风”,”fx1″:”微风”,”fx2″:”微风”,”fl1″:”小于3级”,”fl2″:”小于3级”,”fl3″:”小于3级”,”fl4″:”小于3级转3-4级”,”fl5″:”3-4级转小于3级”,”fl6″:”小于3级”,”index”:”舒适”,”index_d”:”建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。”,”index48″:”舒适”,”index48_d”:”建议着薄型套装等春秋过渡装。年老体弱者宜着套装。但昼夜温差较大,注意适当增减衣服。”,”index_uv”:”最弱”,”index48_uv”:”最弱”,”index_xc”:”不宜”,”index_tr”:”一般”,”index_co”:”舒适”,”st1″:”18″,”st2″:”9″,”st3″:”24″,”st4″:”12″,”st5″:”26″,”st6″:”12″,”index_cl”:”不宜”,”index_ls”:”不宜”}}

1、变量名解释

temp{数字}:以date_y为基准未来六天天气情况,以摄氏度为准。

tempF{数字}:跟temp{数字}一样,只不过是以华氏度为准。

weather{数字}:天气现象。

img{数字}:天气现象的图片ID,这里可以从http://m.weather.com.cn/img/c{天气现象图片ID}.gif获取到一组图片。而每两组图片为一个图片标识分别是:白天和黑夜,因此才会有12组图片。

img_title{数字}:天气现象的图片title值。

wind{数字}:风向。

fx{数字}:风力。

最后是一些指数建议的数据。

2、数据更新频率

好像是每天的6、12、18时会更新一次,因此结合自身的情况同步更新。

整体的情况大概这样,希望整理这篇能给您带来方便。最后获取中国天气网数据是需要授权的,建议您在获取数据时先到http://www.weather.com.cn获取授权证明。

例子:

test.html

View Code
<!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" />
<style type="text/css">
* {margin: 0;padding: 0;list-style:none;font-weight:normal;line-height:180%;}img {border:0;}body {font-family: Verdana, Arial, Helvetica, sans-serif;background-color:transparent;color:#000;font-size:12px;text-align:center;}a {color:#000;text-decoration:none;}a:hover {color:#FF6600;text-decoration:underline;}em {font-style:normal;}strong {}a strong {}select {height:20px;line-height:20px;}ul.row {clear:both;}ul.row li {float:left;margin:0 4px;}ul.row li a {display:inline;height:22px;line-height:22px;}.more a {}ul.col li {margin:4px auto;text-align:center;font-size:14px;font-weight:bold;}ul.col li #city {font-weight:bold;margin-left:10px;}ul.col li a {font-size:14px;font-weight:bold;}ul.col .small {padding-top:6px;}ul.col .small a {font-size:12px;}ul.col .line {padding:10px;border-bottom:1px dashed #ddd;}dl {clear:both;margin:0 auto;}dl dt {float:left;height:65px;margin-right:10px;}dl dt strong {display:block;margin-top:5px;*margin-top:10px;_margin-top:15px;}dl dt a {}dl dd {margin-left:5px;text-align:left;height:18px;line-height:18px;padding:0;margin:0;}dl .title {margin-bottom:4px;_margin-bottom:-4px;}dl .title #city {font-size:14px;font-weight:bold;}#cj {clear:both;}#left {float:left;text-align:center;}#left h3 { margin-bottom:4px;}#left h3 #city {font-size:14px;font-weight:bold;}#cj h4 {font-size:12px;margin-bottom:5px;}#right {margin-left:10px;text-align:left;float:left;}#cjbg {background:url(/img/bg.gif) left bottom repeat-x;height:90px;padding:10px;}#cjbg .line {border-left:1px solid #ddd;padding-left:8px;}#cjbg h4 {font-size:12px;}#right h3 #city {font-size:14px;margin-right:10px;font-weight:bold;margin-bottom:4px;}ul.new { background-color:transparent; height:28px; line-height:28px;}ul.new li a img { margin-top:4px;}ul.new li a em {font-weight:bold;color:#fff;line-height:28px;}ul.new li a:hover {text-decoration:none;color:#000;}ul.new li a:hover em {color:#000;}#eNew {text-align:left;margin:0 auto;width:150px;height:180px;}#eNew h1 {font-size:20px;margin-bottom:0;}#eNew h2 a em { font-size:14px; font-weight:normal;}#eNew h4 {background:url(/img/w.gif) left top no-repeat;height:43px;line-height:43px;text-indent:1em;width:150px;}#eNew h4 a em {line-height:43px;font-size:18px;font-weight:blod;}#eNew p {font-size:12px;}.blue{ text-align:right;}.blue li a{ color:#0070C0;}ul.blue li{ float:right;}
</style>
</head>
<body>
<div id="cj">
<div id="left">
<h3><a id="url1" href="" target="_blank"><em id="city"></em></a></h3>
<p><a id="url2" href="" target="_blank"><img id="big1" src="" alt=""/></a>&nbsp;<a id="url3" href="" target="_blank"><img id="big2" src="" alt="" /></a></a></p>
<h4><a id="url4" href="" target="_blank"><em id="weather1"></em></a></h4>
</div>
<div id="right">
<h4>温 度:<a id="url5" href="" target="_blank"><em id="temp1"></em></a></h4>
<h4>风 力:<a id="url6" href="" target="_blank"><em id="wd1"></em></a></h4>
<h4>紫外线:<a id="url7" href="" target="_blank"><em id="index_uv"></em></a></h4>
<h4 class="more"><a id="url8" href="" target="_blank">未来七天预报</a></h4>
</div>
</div>
<script type="text/javascript" src="weatherapi.js"></script>
</body>
</html>

 

  weatherdata.php

$url = $_GET['url'];
$content = file_get_contents($url); 
echo $content;

 

  weatherapi.js

View Code
/* 
 * author xiaoluozi
 * weather api
 */

var xmlhttp = null;
var jsonobj;
var rs = "http://61.4.185.48:81/g/";
var cookie_info = getCookie('newcity1');
var id1;
var ids = "url1,url2,url3,url4,url5,url6,url7,url8,url9,url10,url11,url12,url13";
var url = window.location.href;
var start = url.indexOf("id");
var end = url.indexOf("T");
var colors = url.indexOf("color");
var startc
var weatherUrl = "http://m.weather.com.cn";
if (colors != -1)
 {
    colorstr = url.substring(colors + 5);
    document.getElementById("city").style.color = colorstr;
    document.getElementById("temp1").style.color = colorstr;
}
var setURL = function(ids, url)
 {
    var nodes = ids;
    if (typeof nodes == "string") {
        nodes = nodes.split(",");
    }
    for (var i = 0; i < nodes.length; i++) {
        if (document.getElementById(nodes[i])) {
            document.getElementById(nodes[i]).href = url;
        }
    }
}
function setCookie(name, value)
 {
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    if (expires != null)
     {
        var LargeExpDate = new Date();
        LargeExpDate.setTime(LargeExpDate.getTime() + (expires * 365 * 24 * 60 * 60 * 1000 * 10));
    }
    document.cookie = name + "=" + escape(value) + ((expires == null) ? "": ("; expires=" + LargeExpDate.toGMTString()));
}
function getCookie(Name)
 {
    var search = Name + "=";
    if (document.cookie.length > 0)
     {
        offset = document.cookie.indexOf(search)
         if (offset != -1)
         {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(offset, end));
        }
         else return "";
    }
}
function createXMLHTTPRequext()
 {
    if (window.XMLHttpRequest)
     {
        xmlhttp = new XMLHttpRequest();
    }
     else if (window.ActiveXObject)
     {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        if (!xmlhttp)
         {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}
function HandleStateChange()
 {
    if (xmlhttp.readyState == 4)
     {
        var jsontext = xmlhttp.responseText;
        var func = new Function("return " + jsontext);
        jsonobj = func();
    }
}
function PostOrder(xmldoc)
 {
    createXMLHTTPRequext();
    xmlhttp.open("GET", "source/plugin/xyfx_tianqirobot/weatherdata.php?url="+xmldoc, false);
    xmlhttp.onreadystatechange = HandleStateChange;
    xmlhttp.send(null);
}
function returndata(id) {
    var datastr1;
    if (id == "") {
        str = "101010100";
        datastr1 = weatherUrl + '/data/' + str + '.html';
    } else {
        datastr1 = weatherUrl + '/data/' + id + '.html';
    }
    PostOrder(datastr1);
    HandleStateChange();
    var parseData = new Object();
    with(jsonobj.weatherinfo) {
                parseData = {
                        city: {
                                //innerHTML: city.substring(0, 1).toUpperCase() + city.substring(1)
                                innerHTML: city
                        },
                        weather1: {
                                innerHTML: weather1
                        },
                        temp1: {
                                innerHTML: temp1    //温度
                        },
                        wd1: {
                                innerHTML: fl1   //风力
                        },
                        img1: {
                                innerHTML: img1
                        },
                        img2: {
                                innerHTML: img2
                        },
                        index: {
                                innerHTML: index
                        },
                        index_d: {
                                innerHTML: index_d  //穿衣指数
                        },
                        index_xc: {
                                innerHTML: index_xc
                        },
                        index_uv: {
                                innerHTML: index_uv  //紫外线指数
                        },
                        url8: {
                                href: "http://www.weather.com.cn/weather/" + id + ".shtml#7d"   //更多7天
                        },
                        date_y: {
                                innerHTML: date_y
                        },
                        week: {
                                innerHTML: week
                        },
                        small: {
                                src: weatherUrl + "/img/c" + img_single + ".gif",
                                title: img_title_single
                        },
                        small1: {
                                src: weatherUrl + "/img/c" + img1 + ".gif",
                                title: img_title1
                        },
                        small2: {
                                src: img2 == 99 ? "": weatherUrl + "/img/c" + img2 + ".gif",
                                title: img_title2
                        },
                        big: {
                                src: weatherUrl + "/img/b" + img_single + ".gif",
                                title: img_title_single
                        },
                        big1: {
                                src: weatherUrl + "/img/b" + img1 + ".gif",
                                title: img_title1
                        },
                        big2: {
                                src: (img2 == 99) ? "": weatherUrl + "/img/b" + img2 + ".gif",
                                title: (img2 == 99) ? "": img_title2,
                                width: (img2 == 99) ? "0": "50",
                                alt: (img2 == 99) ? "": weatherUrl + "img_title2"
                        }
                }
        }
    for (var m in parseData) {
        var node = document.getElementById(m);
        var sets = parseData[m];
        if (node) {
            for (var prop in sets) {
                node[prop] = sets[prop];
            }
        }
    }
}
if (start != -1) {
    var first = start + parseInt(3);
    call = url.substring(first, end);
    returndata(call);
    setURL(ids, "http://www.weather.com.cn/weather/" + call + ".shtml");
}
 else {
    if (!cookie_info)
     {
        var js = document.createElement("script");
        js.setAttribute("type", "text/javascript");
        js.setAttribute("src", rs);
        document.body.insertBefore(js, null);
        function id_callback()
         {
            std = id;
            if (typeof(id) == "undefined")
             {
                id1 = "101010100";
                setURL(ids, "http://www.weather.com.cn/weather/" + id1 + ".shtml");
                returndata(id1);
            }
             else
             {
                id1 = std;
                time = new Date();
                time.setTime(time.getTime() + 365 * 24 * 60 * 60 * 1000 * 10);
                date = time.toGMTString();
                document.cookie = "newcity1=" + escape(std) + ";expires=" + date;
                setURL(ids, "http://www.weather.com.cn/weather/" + id1 + ".shtml");
                returndata(std);
            }
        }
    } else {
        id1 = cookie_info;
        setURL(ids, "http://www.weather.com.cn/weather/" + id1 + ".shtml");
        returndata(id1);
    }
}
posted @ 2012-05-10 11:55  xiaoluozi513  阅读(870)  评论(0编辑  收藏  举报