Skip to content

Instantly share code, notes, and snippets.

@matthlavacka
Created October 25, 2018 22:55
Show Gist options
  • Select an option

  • Save matthlavacka/11140cd198be799d5925b8e8a74b6de6 to your computer and use it in GitHub Desktop.

Select an option

Save matthlavacka/11140cd198be799d5925b8e8a74b6de6 to your computer and use it in GitHub Desktop.

Revisions

  1. matthlavacka created this gist Oct 25, 2018.
    31 changes: 31 additions & 0 deletions server_google_search.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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();
    });