0

I'm making a greasemonkey script to extract video URL code from youtube result: I have this:

// ==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);
}

I use xmlHttpRequest to retrieve the content of youtube, but the code of the page present in the response is not complete. There is a form to retrieve the complete source code of a page in javascript?

fpilee
  • 1,918
  • 2
  • 22
  • 38
  • 2
    why aren't you using youtube API to get results in JSON? – charlietfl Jan 13 '13 at 15:17
  • Use the API. If you insist on doing it the hard way see [How to get an AJAX get-request to wait for the page to be rendered before returning a response?](http://stackoverflow.com/q/11486256/331508). – Brock Adams Jan 13 '13 at 20:38

1 Answers1

2

It seems you are receiving the whole source code of the page. The problem is that the page is using AJAX calls of its own to further load other resources, thus leaving you with an "incomplete" version of the page.

I recommend using the YouTube API for YouTube integration instead of scraping the HTML.

I've never used it myself, so I cannot give you a quick reference of it, but I'm sure it's quite simple to use. :) Hope it helps!

Brock Adams
  • 90,639
  • 22
  • 233
  • 295