javascript - 在 javascript 中检索页面的源代码

标签 javascript xmlhttprequest cross-domain greasemonkey

我正在制作一个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/

相关文章:

javascript - Safari 弹出窗口无法打开 - 执行 window.open() 时未返回对象

javascript - XMLHttpRequest 似乎没有做任何事情

javascript - 如何使用javascript获取基于url的源代码?

flash - 跨域XML和Flash

ajax - 我认为允许跨子域 AJAX 请求,但这个 Chrome 错误似乎表明不是这样

javascript - 下一个JS : Right way to fetch client-side only data in a Functional Component

javascript - 推特 Bootstrap : Getting the index of an open tab

json - 如何使用 d3.js 同步加载 JSON 数据?

Javascript 受客户端和 access-control-allow-origin 限制

javascript - 将 Google Places Autocomplete pac-container 与输入字段相关联