Skip to content

Instantly share code, notes, and snippets.

@matthlavacka
Created October 25, 2018 22:55
Show Gist options
  • Save matthlavacka/11140cd198be799d5925b8e8a74b6de6 to your computer and use it in GitHub Desktop.
Save matthlavacka/11140cd198be799d5925b8e8a74b6de6 to your computer and use it in GitHub Desktop.
Search google using casper
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
casper.start('http://google.fr/', function() {
// Wait for the page to be loaded
this.waitForSelector('form[action="/search"]');
});
casper.then(function() {
// search for 'drake' from google form
this.fill('form[action="/search"]', { q: 'drake' }, true);
});
casper.then(function() {
// aggregate results for the 'drake' search
links = this.evaluate(getLinks);
});
casper.run(function() {
// echo results in some pretty fashion
this.echo(links.length + ' links found:');
this.echo(' - ' + links.join('\n - ')).exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment