0

我正在制作一个greasemonkey脚本来从youtube结果中提取视频URL代码:我有这个:

// ==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 中检索页面的完整源代码吗?

4

1 回答 1

2

看来您正在接收页面的整个源代码。问题是页面使用它自己的 AJAX 调用来进一步加载其他资源,从而给您留下一个“不完整”的页面版本。

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

我自己从来没有使用过它,所以我不能给你一个快速的参考,但我相信它使用起来很简单。:) 希望能帮助到你!

于 2013-01-13T15:20:23.273 回答