我正在制作一个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中检索页面的完整源代码吗?
最佳答案
看来您正在收到该页面的完整源代码。问题在于该页面正在使用自己的 AJAX 调用来进一步加载其他资源,从而给您留下一个“不完整”版本的页面。
我建议使用the YouTube API用于 YouTube 集成,而不是抓取 HTML。
我自己从未使用过它,所以我无法给你一个快速引用,但我确信它使用起来非常简单。 :) 希望有帮助!
关于javascript - 在 javascript 中检索页面的源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14304719/