Skip to content

Instantly share code, notes, and snippets.

@n1k0
Last active February 22, 2020 17:50
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save n1k0/5477539 to your computer and use it in GitHub Desktop.
Save n1k0/5477539 to your computer and use it in GitHub Desktop.
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('http://www.google.com/', 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 [].map.call(nodes, function(node){
          return node.textContent;
      });
  });
});

casper.run(function() {
  this.echo(suggestions.join('\n')).exit();
});

Usage:

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

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

Profit.

@shavit
Copy link

shavit commented Oct 13, 2014

Maybe the markup has changed

casper.waitFor(function() {
  return this.fetchText('.gstl_0 li .sbqs_c');
}, function() {
  suggestions = this.evaluate(function() {
      var nodes = document.querySelectorAll('.gstl_0 li .sbqs_c');
      return [].map.call(nodes, function(node){
          return node.textContent;
      });
  });
});

@bricemaurin
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