Created
October 25, 2018 22:55
-
-
Save matthlavacka/11140cd198be799d5925b8e8a74b6de6 to your computer and use it in GitHub Desktop.
Search google using casper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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