我正在創建一個casperjs
腳本來解析網頁中的數據。我正在使用jQuery以及casperjs
。jQuery返回正確數量的元素,但只有第一個是對象
我遇到的問題是,當我嘗試循環遍歷article
元素時,除第一個元素外,其他元素都是null
。腳本報告頁面上正確的元素數量。
我的代碼如下(我已經刪除了部分腳本,因爲它不是必需的)。
var articles = null;
/*
* Parse all article elements
*/
casper.then(function() {
console.log('Parsing articles from feed ...');
articles = this.evaluate(function() {
return $('article');
});
console.log(typeof(articles)); // prints 'object'
// Prints 56
console.log(articles.length);
});
/*
* Parse data from article elements
*/
casper.then(function() {
for(i=0; i<=articles.length; i++)
{
console.log(articles[i]);
}
});
當我運行該腳本,我從for
環以下幾點:
[object Object]
null
null
null
null
null
null
...
任何想法,爲什麼發生這種情況?
可能重複[爲什麼this.evaluate不正確返回DOM節點?](http://stackoverflow.com/questions/26353431/why-doesnt-this-evaluate-return-dom-nodes-correctly ) –