在javascript中检索页面的源代码

时间:2013-01-13 15:05:14

标签: javascript xmlhttprequest cross-domain greasemonkey

我正在制作一个关于从youtube结果中提取视频网址代码的greasemonkey脚本: 我有这个:

// ==UserScript==
// @name        extract
// @namespace   here
// @include     http://stackoverflow.com/
// @grant GM_xmlhttpRequest
// @version     0.1
// ==/UserScript==

window.setTimeout(conseguir_articulos_youtube, 5000);

function conseguir_articulos_youtube(){

    GM_xmlhttpRequest({
        method: "GET",
        url: "http://www.youtube.com/results?search_sort=video_date_uploaded&uni=3&search_type=videos&search_query=humor",
        onload: on_load_extract              
    });
}

function on_load_extract(data){
    var datos=data.responseText;
    alert(datos);
}

我使用xmlHttpRequest来检索youtube的内容,但响应中存在的页面代码不完整。有一个表单可以在javascript中检索页面的完整源代码吗?

1 个答案:

答案 0 :(得分:2)

您似乎正在收到该页面的完整源代码。问题是页面正在使用它自己的AJAX调用来进一步加载其他资源,从而使你得到一个“不完整”的页面版本。

我建议使用the YouTube API进行YouTube集成,而不是抓取HTML。

我自己从未使用过它,所以我不能给你一个快速的参考,但我确信它的使用非常简单。 :)希望它有所帮助!

相关问题