2013-01-13 53 views
0

我正在做的Greasemonkey脚本从YouTube结果提取视频网址代码: 我有这样的:检索页面的源代码在JavaScript

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

+2

为什么不使用youtube API在JSON中获得结果? – charlietfl

+0

使用API​​。如果你坚持这样做很难,请参阅[如何在返回响应之前获取AJAX get-request等待页面呈现](http://stackoverflow.com/q/11486256/331508)。 –

回答

2

看来你正在接收页面的整个源代码。问题在于该页面正在使用自己的AJAX调用来进一步加载其他资源,从而导致页面的“不完整”版本。

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

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