I'm using jQuery selectors for casper.js scraping. I understand that it's necessary to place the jQuery calls inside casper.evaluate()
.
The problem is that in the last of these following three functions, a ReferenceError: Can't find variable: $
is raised. The first two work absolutely fine.
// 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;
});
}
Here's the rest of the script, with the unimportant details obscured. Note that anotherValidPage
is a valid URL which returns HTTP 200 (success).
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();