2017-07-16 111 views
0

我現在使用casperjs進行網絡爬行。幾乎一切都很好,但我遇到了一些麻煩。首先,我的代碼如下所示。casperjs評估函數返回無效值

casper.start().each(SOME_URLS, function(self, URL) { 
    self.thenOpen(URL, function() { 

     self.then(function() { 
      var getDatas = function() { 
       var title = $('SOME_SELECTOR').map(function() { 
        return $(this).text(); 
       }).get(); 

       return { 
        title: title 
       }; 
      } 

      data = self.evaluate(getDatas); 
      console.log(JSON.stringify(data)); 
     }); 

    }); 
}).run(); 

我想從網頁獲取一些數據到'數據'變量。有時數據非常好(在console.log上),但有時數據是空的!

爲什麼會發生這種情況?我錯了什麼?

+0

它看起來像你使用jQuery,如果你不把它添加到您的卡斯帕用戶代理沒有保證,無論網站你刮加載它。這可能是問題嗎?也可能是您的數據分配按錯誤順序運行,請嘗試將其放入單獨的casper.then()中。另外,請提供鏈接到網站,它的工作原理並不能幫助人們找到答案。 –

回答

0

問題是你不能撥打casper.start不止一次。您的循環需要在casper.start功能內或casper.then

請參閱此excellent SO answer以幫助您執行此操作。

基本上只能調用casper.start一次,把你的循環內casper.then