JS天气预报获取

天气预报数据源

天气预报数据源测试
http://m.weather.com.cn/data/101280601.html 深圳
http://m.weather.com.cn/data/101240601.html 吉安

天气预报数据源获取
http://m.weather.com.cn/data/101240601.html

这里,101240601即为城市id


id怎么获取?
通过 http://61.4.185.48:81/g/ 推荐这个接口
返回: var ip="113.90.74.76";var id=101280601;if(typeof(id_callback)!="undefined"){id_callback();}

通过 http://fw.qq.com/ipaddress
返回: var IPData = new Array("113.90.74.76","","广东省","深圳市");


php方式抓取到本地

<? php 
    
header ( " content-type: text/html " ); 
    
$url   =   ' http://m.weather.com.cn/data/ '
    
$id   =   $_GET [ ' id ' ]; 
    
$data   =   file_get_contents ( $url   .   $id   . ' .html ' ); 
    
echo   $data
?>

asp方式抓取到本地

<
   
on   error   resume   next  
    response.charset
=   ' utf-8';
    server.scripttimeout =   9999999  
   
// 入口函数 
   
function gethttppage(path) 
        t
= getbody(path) 
        gethttppage
= bytestobstr(t, " gb2312 "
   
end function
   
' 首先,进行小偷程式的一些初始化设置,以上代码的作用分别是忽略掉所有非致命性错误,把小偷程式的运行超时时间设置得非常长(这样不会出现运行超时的错误),转换原来默认的utf-8编码转换成gb2312编码,否则直接用xmlhttp组件调用有中文字符的网页得到的将是乱码。
    function getbody(url) 
       
on   error   resume   next  
       
set retrieval =   createobject ( " microsoft.xmlhttp "
       
with retrieval 
            .open
" get " , url, false , "" , ""  
            .send 
            getbody
= .responsebody 
       
end   with  
       
set retrieval =   nothing  
   
end function
   
' 然后调用xmlhttp组件创建一个对象并进行初始化设置。
    function bytestobstr(body,cset) 
       
dim objstream 
       
set objstream = server.createobject( " adodb.stream "
        objstream.type
=   1  
        objstream.mode
= 3  
        objstream.open 
        objstream.write body 
        objstream.position
=   0  
        objstream.type
=   2  
        objstream.charset
= cset 
        bytestobstr
= objstream.readtext 
        objstream.close 
       
set objstream =   nothing  
   
end function  
    
   
// 获取匹配的字符 
   
function newstring(wstr,strng) 
        newstring
= instr ( lcase (wstr), lcase (strng)) 
       
if newstring <= 0   then newstring = len (wstr) 
   
end function  
%
>
<
   
' 处理抓取回来的数据需要调用adodb.stream组件并进行初始化设置。以下即为页面显示部分
    dim wstr, url, start, over, city, body
   
' 定义一些需要使用到的变量
    city = request.querystring( " id "
    
   
' 程序传回的id变量(即用户选择的城市)赋给id 
    url = " http://m.weather.com.cn/data/ " & cityId & " .html " ;  
   
' 这里设置需要抓取的页面地址,获取指定页面的全部数据
    wstr = gethttppage(url)
    response.Write(wstr) 
%
>


JS加载数据源

< script type = " text/javascript " >  
   
// 动态加载js
    function loadJs(jsUrl, fCallBack) { 
       
var _script = document.createElement( ' script ' ); 
        _script.setAttribute(
' type ' , ' text/javascript ' ); 
        _script.setAttribute(
' src ' , jsUrl); 
        document.getElementByTagName(
' head ' )[ 0 ].appendChild(_script); 
       
if ( typeof fCallBack !=   ' undefined ' ) { 
           
// if ($.browser.msie) //借助jQuery判断ie
            if ($.browser.msie && ($.browser.version ==   " 6.0 " ) &&   ! $.support.style) { 
                _script.onreadystatechange
=   function () { 
                   
if ( this .readyState ==   ' loaded '   ||   this .readyState ==   ' complete ' ) { 
                        fCallBack(); 
                    } 
                }; 
            }
else
                _script.onload
=   function () { 
                    fCallBack(); 
                }; 
            } 
        } 
    } 
    
   
var cityIdUrl =   ' http://61.4.185.48:81/g/ '
   
var weatherUrl =   ' getWeather.asp?id= ' ; // 因为js的跨域问题,所以通过其他方式拿
    /*  
    loadJs(cityIdUrl, function() { 
        if (typeof id != 'undefined') { 
            var curDayWeather; //先出缓存中获取 
            $.getJSON(weatherUrl + id, function(data) { 
                var w_info = data.weatherinfo; 
                curDayWeather = w_info.weather1; 
                //下面可以尽情地解析获取到的天气数据 
            }); 
        } 
    });
*/   // 为了方便我把它封成回调形式
    // 演变后:
    function getWeather =   function (f_cb) { 
        loadJs(cityIdUrl,
function () { 
           
if ( typeof id !=   ' undefined ' ) { 
               
var curDayWeather; //可以在此添加缓存机制
                // 根据不同城市的id获取它的天气预报
                $.getJSON(weatherUrl + id, function (data) { 
                   
var w_info = data.weatherinfo; 
                    curDayWeather
= w_info.weather1; // json对象
                    // 下面可以尽情地解析获取到的天气数据,如果要获取近几天和未来的数据可以在此进行扩展
                    f_cb(curDayWeather); 
                }); 
            } 
        }); 
    }
   
// 调用之
    getWeather( function (weather_data) { 
       
// 这里把拿到的weather_data放到你想要的位置。
    }); 
< / script>

转载于:https://www.cnblogs.com/leamiko/archive/2011/08/19/2145746.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值