Skip to content

Instantly share code, notes, and snippets.

Last active February 22, 2020 17:50
What would you like to do?
A simple CasperJS script to fetch google suggestions from a partial search

CasperJS Google Suggest

The script:

/*global casper:true*/
var casper = require('casper').create({
    pageSettings: {
        userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Firefox/31.0"
var suggestions = [];
var word = casper.cli.get(0);

if (!word) {
    casper.echo('please provide a word').exit(1);

casper.start('', function() {
    this.sendKeys('input[name=q]', word);

casper.waitFor(function() {
  return this.fetchText('.gsq_a table span').indexOf(word) === 0
}, function() {
  suggestions = this.evaluate(function() {
      var nodes = document.querySelectorAll('.gsq_a table span');
      return [], function(node){
          return node.textContent;
}); {


$ casperjs google-suggest.js why
why not
why not productions
why paris
why don't you do right
why not sushi
why why love
why so serious

Works as well with sentences actually:

$ casperjs google-suggest.js "why do unicorns"
why do unicorns have horns
why do unicorns exist
why do unicorns drink
why do unicorns poop rainbows
why do unicorns not exist
why do unicorns fart rainbows
why do unicorns like virgins
why do unicorns fly
why do unicorns have wings
why do unicorns eat


Copy link

Hi there,
With the script above, I only get 4 results. What did you change to get 10?

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment