zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    hive报错 java.sql.SQLException: No suitable driver found for jdbc:hive://localhost:10000/default
    使用Beeline连接Hive
    hive报错 root is not allowed to impersonate root (state=08S01,code=0)
    hive报错 Could not open client transport with JDBC Uri: jdbc:hive2://node01:10000/default:java.net.ConnectException refused
    excel快速删除空值单元格,数据上移
    FineBI 图表选择
    数据库连接池大小设置?
    工作中有待留❤️积累的一些经验
    内存包括随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE)。RAM最重要
    我自己ood的复习思路:抄
  • 原文地址:https://www.cnblogs.com/leamiko/p/2145746.html
Copyright © 2011-2022 走看看