Amazon Login Phantom JS

2019-08-07 03:16发布

问题:

I’m pretty new at PhantomJS and programming in general, so please bear with me. I’m trying to write a code to login to my Amazon account, and add shipping addresses to my address book. The code I’m using is here:

var steps=[];
var loadInProgress = false;//This is set to true when a page is still loading

/*********SETTINGS*********************/


var webPage = require('webpage');
var page = webPage.create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)      AppleWebKit/537.36     (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.loadImages = false;//Script is much faster with this field set to false
phantom.cookiesEnabled = true;
phantom.javascriptEnabled = true;

/*********SETTINGS END*****************/

console.log('All settings loaded, start with execution');  
page.onConsoleMessage = function(msg) {
    console.log(msg);
};
/**********DEFINE STEPS THAT FANTOM SHOULD DO***********************/
steps = [

     //Step 1 - Open Amazon home page
     function(){
         console.log('Step 1 - Open Amazon home page');
         page.open("https://www.amazon.com/gp/css/account/address/view.html?ie=UTF8&ref_=myab_view_new_address_form&viewID=newAddress&", function(status){      
         });
     },

     //Fill out login info
     function(){
         console.log('Step 2 - Populate and submit the login form');
         page.evaluate(function(){
             document.getElementById("ap_email").value= "my email";
             document.getElementById("ap_password").value="my password";
             document.getElementById("signInSubmit").click();
         });
     },

     //Write out webpage
         function(){
             var fs = require('fs');
             var result = page.evaluate(function() {
                     return document.querySelectorAll("html")[0].outerHTML;
                 });
             fs.write('AmazonPage1.html',result,'w');

         },

     //Fill out shipping info
         function(){
             console.log('Step 3 - Populate and submit the shpping info');
             page.evaluate(function(){

                 document.getElementById("enterAddressFullName").value= "name";
                 document.getElementById("enterAddressAddressLine1").value="address";
                 document.getElementById("enterAddressCity").value="city";
                 document.getElementById("enterAddressStateOrRegion").value="state";
                 document.getElementById("enterAddressPostalCode").value="zip";
                 document.getElementById("enterAddressPhoneNumber").value="phone";

                 document.getElementById("myab_newAddressButton").click();
             });

     },

     //Write out webpage
         function(){
             var fs = require('fs');
             var result = page.evaluate(function() {
                     return document.querySelectorAll("html")[0].outerHTML;
                 });
             fs.write('AmazonPage2.html',result,'w');

         }

     ];
/**********END STEPS THAT FANTOM SHOULD DO***********************/



//Execute steps one by one
interval = setInterval(executeRequestsStepByStep,50); 

var testindex = 0;

function executeRequestsStepByStep(){
    if (loadInProgress == false && typeof steps[testindex] == "function" ) {
        steps[testindex]();
        testindex++;
    }

    if (typeof steps[testindex] != "function") {
        console.log("test complete!");
        phantom.exit();
    }

}

/**
 * These listeners are very important in order to phantom work properly. Using these listeners, we control loadInProgress marker which controls, weather a page is fully loaded.
 * Without this, we will get content of the page, even a page is not fully loaded.
 */
page.onLoadStarted = function() {
    loadInProgress = true;
    console.log('Loading started');
};
page.onLoadFinished = function() {
    loadInProgress = false;
    console.log('Loading finished');
};
page.onConsoleMessage = function(msg) {
    console.log(msg);
};

Sometimes it works, but most of the time I get a captcha or a message from Amazon saying “Please Enable Cookies to Continue”. This post (Amazon Seller Central Login Scrape PhantomJS + CasperJS) seems to maybe have some solution but I’m having a little trouble making sense of it. Can anyone explain in somewhat more lay terms how to accomplish this without getting any problems from Amazon?

Also a few other questions if you don’t mind: I’m running this code locally from my home… so is my IP address now on some Amazon “suspicious IP list"? Can/would they ban my account for this??? I’d hate to lose my account over something like this. Maybe I should use a proxy? Is there something better than PhantomJS to use for this?

Sorry for the loaded question and thanks!

回答1:

I think amazon sends cookie via images too. So don't disable image loading. Make sure you are moving from one page to another only after the page loaded completely. Here is some suggestions for the captcha issue.