Skip to content

Instantly share code, notes, and snippets.

@observerss
Last active August 6, 2023 12:03
Show Gist options
  • Select an option

  • Save observerss/3798922 to your computer and use it in GitHub Desktop.

Select an option

Save observerss/3798922 to your computer and use it in GitHub Desktop.

Revisions

  1. observerss revised this gist Oct 21, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // EDIT: 2013/10/20
    // google has updated its kwt UI, this script doesn't work any more!
    // may be I will update this script if when I have time to investigate their new Interface.
    // may be I will update this script when I have time to investigate their new Interface.


    // requires
  2. observerss revised this gist Oct 21, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # EDIT: 2013/10/20
    # google has updated its kwt UI, this script doesn't work any more!
    # may be I will update this script if when I have time to investigate their new Interface.
    // EDIT: 2013/10/20
    // google has updated its kwt UI, this script doesn't work any more!
    // may be I will update this script if when I have time to investigate their new Interface.


    // requires
  3. observerss revised this gist Oct 21, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # EDIT: 2013/10/20
    # google has updated its kwt UI, this script doesn't work any more!
    # may be I will update this script if when I have time to investigate their new Interface.


    // requires
    var utils = require('utils');
    var casper = require('casper').create()
  4. observerss revised this gist Oct 18, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ function start(kwurl) {

    function query(kws){
    this.thenEvaluate(function(kws){
    document.querySelector('.sEAB').textContent = kws.join('\n');
    document.querySelector('.sEAB').value = kws.join('\n');
    document.querySelector('button.gwt-Button').click();
    }, {kws:kws});
    }
  5. observerss revised this gist Sep 28, 2012. No changes.
  6. observerss created this gist Sep 28, 2012.
    93 changes: 93 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    // requires
    var utils = require('utils');
    var casper = require('casper').create()
    var casper = require('casper').create({
    verbose: true,
    logLevel: "debug"
    });

    // setup globals
    var email = casper.cli.options['email'] || 'REPLACE THIS EMAIL';
    var passwd = casper.cli.options['passwd'] || 'REPLACE THIS PASSWORD';
    var keywords = casper.cli.options['keywords'] || 'iphone,cars,kindle';
    keywords = keywords.split(',');

    var kwurl = ''
    var result = {}
    var kws_list = [];
    for (var j=0; j<((keywords.length-1)/50+1); j++) {
    kws_list.push( keywords.slice(j*50, j*50+50) );
    }

    // login & save url
    casper.start('http://adwords.google.com');

    casper.thenEvaluate(function login(email, passwd) {
    document.querySelector('#Email').setAttribute('value', email);
    document.querySelector('#Passwd').setAttribute('value', passwd);
    document.querySelector('form').submit();
    }, {email:email, passwd:passwd});


    casper.waitForSelector(".aw-cues-item", function() {
    kwurl = this.evaluate(function(){
    var search = document.location.search;
    return 'https://adwords.google.com/o/Targeting/Explorer'+search+'&__o=cues&ideaRequestType=KEYWORD_IDEAS';
    })
    //this.open(kwurl);
    });

    // control flows
    function start(kwurl) {
    this.start(kwurl); // we should start a new page,
    // don't worry, we are still logged in
    // ``this.open`` won't work here
    this.waitForSelector("button.gwt-Button", function(){});
    }


    function query(kws){
    this.thenEvaluate(function(kws){
    document.querySelector('.sEAB').textContent = kws.join('\n');
    document.querySelector('button.gwt-Button').click();
    }, {kws:kws});
    }

    function parseresults(){
    this.waitForSelector('#gwt-debug-column-GLOBAL_MONTHLY_SEARCHES-row-0-3', function() {
    var o = this.evaluate(function(){
    var o = {};
    var els = document.querySelectorAll('a.sCM');
    for (var i=0; i<els.length; i++){
    var el = els[i];
    pel = el.parentElement.parentElement.parentElement.parentElement.parentElement;
    o[el.textContent] = [pel.nextSibling.nextSibling.textContent, pel.nextSibling.nextSibling.nextSibling.textContent];
    };
    return o
    });
    for (var k in o){
    result[k] = o[k];
    }
    });
    }

    // recusively call so that it runs sequentially
    var current_kws = 0;
    function check_kws(){
    if (kws_list[current_kws] && kws_list[current_kws].length>0){
    var kws = kws_list[current_kws];

    start.call(this, kwurl);
    query.call(this, kws);
    parseresults.call(this);

    current_kws++;
    this.run( check_kws );
    }else{
    this.echo("All done");
    utils.dump(result);
    this.exit()
    }
    };

    casper.run(check_kws)