0
我正在使用jQuery選擇器進行casper.js抓取。我知道這是必要place the jQuery calls inside casper.evaluate()
。CasperJS找不到jQuery
的問題是,在這三個功能最後,一個ReferenceError: Can't find variable: $
提高。前兩項工作絕對沒問題。
// On main page, scrape links to sub-pages.
function getLinks() {
var links = $('li.ds-artifact-item a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
// On main page, scrape sub-pages' titles.
function getTitles() {
var titles = $('li.ds-artifact-item a');
return Array.prototype.map.call(titles, function(e) {
return e.innerHTML;
});
}
// On sub-page, scrape document description.
function getDescription(){
var descriptions = $('td.label-cell:contains(date)');
return Array.prototype.map.call(descriptions, function(e) {
return e.innerHTML;
});
}
下面是腳本的其餘部分,其中不重要的細節被遮蓋了。請注意0是一個返回HTTP 200(成功)的有效URL。
var links = []; var titles = []; var descriptions = [];
casper.start(validPage, function() {
links = this.evaluate(getLinks);
titles = this.evaluate(getTitles);
});
casper.then(function() {
// echo results
this.echo(links.length + ' links found:');
this.echo(' - ' + links.join('\n - '));
this.echo(titles.length + ' titles found:');
this.echo(' - ' + titles.join('\n - '));
});
casper.thenOpen(anotherValidPage, function(){});
casper.then(function(){
// This call is the problematic one.
descriptions = this.evaluate(getDescription());
this.echo(descriptions.length + ' descriptions found:');
this.echo(' - ' + descriptions.join('\n - '));
});
casper.run();