2013-01-24 70 views
0

我想按照這個教程與節點和jQuery刮 -與node.js中和jQuery刮

http://net.tutsplus.com/tutorials/javascript-ajax/how-to-scrape-web-pages-with-node-js-and-jquery/

內,他們有一些代碼,內容是這樣的 -

request({uri:"http://events.sfgate.com/search?swhat=&swhen=&swhere=San+Francisco&commit=Search&st_select=any&search=true&svt=text&srss="},function(err,response,body){ 

jsdom.env({ 
html: "http://events.sfgate.com/search?swhat=&swhen=&swhere=San+Francisco&commit=Search&st_select=any&search=true&svt=text&srss=", 
src:['http://code.jquery.com/jquery-1.6.min.js'], 
done: function(errors,window){ 
    console.log("WINDOW"); 
    console.log(window.jQuery); 
    var $ = window.$; 
    //other stuff 

當我控制檯日誌window.JQuery,或窗口。$,都是未定義的 - 但不應該是因爲jsdom應該將jquery嵌入到頁面中?爲什麼沒有發生?

回答

0

問題是你用「src」參數初始化它,它應該包含javascript文件的實際源代碼數組(本例中爲jquery) - 而不是文件的url。

如果你想要的網址,你需要初始化它:

jsdom.env(
    "http://nodejs.org/dist/", 
    ["http://code.jquery.com/jquery.js"], 
    function (errors, window) { 

或像這樣:

jsdom.env({ 
    html: "http://news.ycombinator.com/", 
    scripts: ["http://code.jquery.com/jquery.js"], 
    done: function (errors, window) { 

編輯: 。在你的代碼中的另一個錯誤(如果我沒有弄錯......) - 你首先下載請求模塊的頁面,但不是將html源文件傳遞給jsdom(通過傳遞body你從請求)你告訴jsdom再次下載頁面。如果您將jsdom的頁面url作爲html,那麼您無需致電請求模塊。