Skip to content

Instantly share code, notes, and snippets.

@aarti
Created January 21, 2019 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarti/9e4654ea543be69c2fa23c17f9660299 to your computer and use it in GitHub Desktop.
Save aarti/9e4654ea543be69c2fa23c17f9660299 to your computer and use it in GitHub Desktop.
var casper = require('casper').create();
var all_links = [];
var top_kf8_picture_books_url = "http://www.amazon.com/Best-Sellers-Kindle-Store-Text-Pop-Up-Books/zgbs/digital-text/3381090011/#"
var index = 1;
function getLinks() {
var links = document.querySelectorAll('.zg_itemImageImmersion a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href').trim();
});
}
function logLinkElements(element, index, array) {
rank = index+1;
console.log("Rank " + rank + "=" + element);
}
function next20(index) {
link = top_kf8_picture_books_url +index;
this.echo( "Next 20 with "+ link);
this.thenOpen(link, function() {
var wait_for_text = index * 19+".";
this.echo( "Begin waiting for rank" + wait_for_text + "to appear");
this.waitForText(wait_for_text, function() {
this.echo('Found the rank.'+ wait_for_text);
all_links = all_links.concat(this.evaluate( getLinks ));
this.echo( "Ranks Collected so far"+ all_links.length );
this.echo( all_links[all_links.length-1]);
index += 1;
if (all_links.length < 100 )
{
next20.call( this , index);
}
else
{
this.echo(all_links.length + ' links found:');
all_links.forEach( logLinkElements );
this.echo('completed').exit();
}
});
this.echo(this.getTitle());
});
}
casper.start();
casper.then(function() {
next20.call( this, index );
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment