簡體   English   中英

使用JSONP從URL網頁使用JavaScript獲取源代碼

[英]Get a source code from URL web page with JavaScript using JSONP

我正在嘗試使用JSONP從URL網頁獲取源代碼。 這是代碼:

<script type="text/javascript">
var your_url = '';

$(document).ready(function(){
jQuery.ajax = (function(_ajax){

var protocol = location.protocol,
    hostname = location.hostname,
    exRegex = RegExp(protocol + '//' + hostname),
    YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
    query = 'select * from html where url="{URL}" and xpath="*"';

function isExternal(url) {
    return !exRegex.test(url) && /:\/\//.test(url);
}

return function(o) {

    var url = o.url;

    if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {
        // Manipulate options so that JSONP-x request is made to YQL

        o.url = YQL;
        o.dataType = 'json';

        o.data = {
            q: query.replace(
                '{URL}',
                url + (o.data ?
                    (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                : '')
            ),
            format: 'xml'
        };

        // Since it's a JSONP request
        // complete === success
        if (!o.success && o.complete) {
            o.success = o.complete;
            delete o.complete;
        }

        o.success = (function(_success){
            return function(data) {

                if (_success) {
                    // Fake XHR callback.
                    _success.call(this, {
                        responseText: data.results[0]
                            // YQL screws with <script>s
                            // Get rid of them
                            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                    }, 'success');
                }

            };
        })(o.success);

    }

    return _ajax.apply(this, arguments);

};

})(jQuery.ajax);

$.ajax({
    url: your_url,
    type: 'GET',
    success: function(res) {
         var text = res.responseText;
         //document.getElementById("contenuto").innerHTML = text;

    alert(text);
}
});


});
</script>

我用警告打印了URL中的所有源代碼。

alert(text);

首先,如何知道打印的代碼是否是頁面的所有Web代碼? 如果我嘗試這樣做

document.getElementById("contenuto").innerHTML = text;

結果是:

\ \ <'+'/ins>\ \ \ '); } ]]>

我試圖使用HTML DOM僅打印一個元素,這樣做

 document.getElementById("contenuto").innerHTML = text;
 var elem = text.getElementById("strip_adv").innerHTML;
 document.getElementById("contenuto_1").innerHTML = elem;

}

但這是JS控制台上的錯誤:

text.getElementById is not a function

回顧:我將使用JSONP從URL獲取網頁的源代碼。 我將從返回的文本中使用HTML DOM,以僅保留我需要的元素/類。 我是JS的新手,我試圖了解有關JS的更多信息。

getElementById()僅存在於文檔對象中。 您嘗試做的是嘗試從字符串對象訪問getElementId。

相反,我建議在iframe中插入返回的html字符串,然后您可以訪問iframe中的元素, 否則可以在應用程序中使用某種html解析器。

可以說,在iframe中插入html字符串后,您的html看起來像這樣

<body>
    <iframe id="one">
      <html>
        <body> <h1 id="strip_adv">Heading</h1> </body>
      </html
    </iframe>
</body>

function iframeObj( frameEle ) {
    return frameEle.contentWindow
        ? frameEle.contentWindow.document
        : frameEle.contentDocument
}

var element = iframeObj( document.getElementById('strip_adv') );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM