CasperJS在访问Google关键字规划师时遇到问题

时间:2016-04-08 08:51:21

标签: web-scraping phantomjs casperjs freeze google-adwords

我的目标是从Google关键字规划师中获取关键字,因为API不支持在过去24个月内获得搜索量,但仅支持12个月。

我使用maily SimpleBrowser,所以我是CasperJS的新手,我搜索了一些脚本,阅读了文档,然后合并了以下脚本。

我可以登录谷歌,甚至登录到Adwords仪表板,但是当我尝试访问KeywordPlanner CasperJS冻结时,有什么想法吗?

JS脚本

/**
 * Basic vars
 * @type Module utils|Module utils
 */
var utils = require('utils');
var casper = require('casper').create({
    verbose: true,
    logLevel: "debug",
    waitTimeout: 20000
});

/**
 * Start casper and, set the biggest viewport and browser signature
 */
casper.start();
casper.viewport(1280, 1024);
casper.userAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36');

/**
 * Override user agent when requesting source
 * 
 * @param {type} param1
 * @param {type} param2
 */
casper.on('resource.requested', function(resource) {

    for (var obj in resource.headers) {
        var name = resource.headers[obj].name;
        var value = resource.headers[obj].value;
        if (name == "User-Agent"){
            this.echo(value);
        }
    }

});

/**
 * Prevent locking navigation
 */
casper.on('navigation.requested', function (url, navigationType, navigationLocked, isMainFrame) {
    if (url.indexOf('adwords.google.com/ko/KeywordPlanner/Home?__') != -1 || url.indexOf('facebook.com') != -1) {
      this.page.navigationLocked = true;
    }else{
      this.page.navigationLocked = false;
    }
});

/**
 * Main script
 * 
 * @param {type} param
 */
casper.open("https://accounts.google.com/ServiceLogin?service=adwords&continue=https://adwords.google.com/um/identity?#identifier").then(function () {

    console.log("page loaded...");

    this.fillSelectors('form#gaia_loginform', {
        'input[name="Email"]': 'myemail@gmail.com',
    }); //Fills the email box with email
    console.log("email filled...");

    this.click("#next"); //Fills the email box with email
    console.log("next pressed...");


    this.wait(500, function () { //Wait for next page to load
        console.log("Inside WAIT...");

        this.waitForSelector("#Passwd", //Wait for password box

                function success() {
                    console.log("SUCCESS...");
                    this.fillSelectors('form#gaia_loginform', {
                        'input[name="Passwd"]': 'myPassword',
                    }); //Fill password box with PASSWORD
                    this.click("#signIn"); //Click sign in button
                    console.log("Clicked to sign in...");

                    this.wait(5000, function () {

                        console.log("Logged in...");
                        this.capture('1.png');

                        /**
                         * Open the keyword planner
                         */
                        casper.open('https://adwords.google.com/ko/KeywordPlanner/Home?').then(function() {

                             this.wait(10000, function() {
                                console.log("KP opened...");
                                this.capture('2.png');
                            });

                        });

                    });


                },
                function fail() {
                    console.log("FAIL...");
                    this.capture('exit.png');
                }

        );

    });
});

casper.run();

Casper冻结时的最后一个控制台输出

[debug] [phantom] url changed to 

"https://adwords.google.com/ko/KeywordPlanner/H
ome?"
[debug] [phantom] Navigation requested: url=https://adwords.google.com/um/identi
ty?dst=/ko/KeywordPlanner/Home?, type=Other, willNavigate=true, isMainFrame=true

Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] Navigation requested: url=https://adwords.google.com/ko/Keywor
dPlanner/Home?__u=6961347906&__c=8635630266&authuser=0, type=Other, willNavigate
=true, isMainFrame=true
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] url changed to "https://adwords.google.com/ko/KeywordPlanner/H
ome?__u=6961347906&__c=8635630266&authuser=0"
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigat
e=false, isMainFrame=false
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.110 Safari/537.36

0 个答案:

没有答案