我在PhantomJS和程序设计领域还很陌生,所以请多多包涵。我正在尝试编写代码以登录到我的亚马逊帐户,并将送货地址添加到我的地址簿中。我正在使用的代码在这里:
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);
};
有时它可以工作,但是大多数时候我会收到验证码或来自亚马逊的消息,说“请启用Cookies才能继续”。这篇文章(Amazon Seller Central Login Scrape PhantomJS + CasperJS)似乎有解决方案,但我在理解它时遇到了一些麻烦。任何人都可以用一些比较通俗易懂的方式来解释如何做到这一点,而不会遇到来自亚马逊的任何问题?
如果您不介意的话,还有其他一些问题:
我正在家里从本地运行此代码…我的IP地址现在在某些Amazon“可疑IP列表”上吗?他们可以/是否会为此禁用我的帐户? 。也许我应该使用代理服务器?有什么比PhantomJS更好的东西吗?
很抱歉加载的问题,谢谢!
最佳答案
我认为亚马逊也通过图像发送cookie。因此,请不要禁用图像加载。确保仅在页面完全加载后才从一页移动到另一页。这是验证码问题的some suggestions。
关于javascript - 亚马逊登录Phantom JS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35852091/