2014-11-04 33 views
0

因此,我正在編寫一個帶有jsdom和jquery的簡單頁面抓取程序,並且遇到了一個我不確定如何解決的問題。在Node中共享變量作用域jsdom和jquery

下面是一些代碼,工程(改變URL):

var jsdom = require("jsdom"); 
var fs = require('fs'); 
var jquery = fs.readFileSync("./js/jquery-min.js").toString(); 

//There's two pages of product, here's page 1 
jsdom.env({ 
     url: 'http://exampleshoppingpage.com', 
     src: [ jquery ], 
     done: function(error, window){ 
       var $ = window.$; 
       $('.productlist .product .title a').each(function() { 
         console.log($(this).text()); 
       }); 
     } 
}); 

//And do the exact same thing for page 2 
jsdom.env({ 
     url: 'http://exampleshoppingpage.com?page=2', 
     src: [ jquery ], 
     done: function(error, window){ 
       var $ = window.$; 
       $('.productlist .product .title a').each(function() { 
         console.log($(this).text()); 
       }); 
     } 
}); 

但我真的很想做的就是讓所有這些產品,並將其打印出來之前排序。以下是我的嘗試:

var jsdom = require("jsdom"); 
var fs = require('fs'); 
var jquery = fs.readFileSync("./js/jquery-min.js").toString(); 
var products = []; 


//There's two pages of product, here's page 1 
jsdom.env({ 
     url: 'http://exampleshoppingpage.com', 
     src: [ jquery ], 
     done: function(error, window){ 
       var $ = window.$; 
       products $('.productlist .product .title a').each(function() { 
         products.push($(this).text()); 
       }); 
     } 
}); 

//And do the exact same thing for page 2 
jsdom.env({ 
     url: 'http://exampleshoppingpage.com?page=2', 
     src: [ jquery ], 
     done: function(error, window){ 
       var $ = window.$; 
       $('.productlist .product .title a').each(function() { 
         products.push($(this).text()); 
       }); 
     } 
}); 

products = products.sort(); 
console.log (products.join("\n")); 

我得到一個空數組。我嘗試了其他幾種方法來確定我是不是在做一些愚蠢的事情。我假設它與jsdom中的jQuery沒有與程序的外部分享範圍有關?

回答

1

這是一個我們必須記住異步思考的例子。你的範圍是好的,但你試圖在數據填充之前將products轉儲到控制檯。

另外,Array.prototype.sort()operates on the array directly。它不返回數組。

var jsdom = require("jsdom"); 
var jquery = "http://code.jquery.com/jquery.js"; 

var products = []; 

// page 1 
jsdom.env({ 
     url: 'http://news.ycombinator.com/', 
     scripts: [ jquery ], 
     done: function(error, window){ 
       var $ = window.$; 
       $('td.title:not(:last) a').each(function() { 
         products.push($(this).text()); 
       }); 
       //  page 2 
       jsdom.env({ 
         url: 'https://news.ycombinator.com/news?p=2', 
         scripts: [ jquery ], 
         done: function(error, window){ 
           var $ = window.$; 
           $('td.title:not(:last) a').each(function() { 
             products.push($(this).text()); 

           }); 
           products.sort(); 
           console.log(products); 
         } 
       }); 
     } 
}); 
+0

這也是javascript [瘋狂和不可讀](http://callbackhell.com/)可以得到的一個很好的例子,以及[Promises將使生活變得真棒](http://www.html5rocks .com/en/tutorials/es6/promises /),恕我直言。 – 2014-11-04 17:17:29

+0

嘎!異步思考讓我每次都有!特別是當我從傳統的編程中來回切換時。 – 2014-11-04 18:36:56

+1

我知道!我不能等到它只是所有的JavaScript,所有的時間 – 2014-11-04 19:14:17