我在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);
};
有时会有效,但大部分时间我从亚马逊获得验证码或消息“请启用Cookie继续”。这篇文章(Amazon Seller Central Login Scrape PhantomJS + CasperJS)似乎可能有一些解决方案,但我在理解它时遇到了一些麻烦。任何人都可以用更多的非专业术语来解释如何在不从亚马逊出现任何问题的情况下完成
如果您不介意,还有一些其他问题: 我正在家里本地运行这段代码......现在我的IP地址在某些亚马逊“可疑的IP列表”上了吗?可以/他们会禁止我的帐户吗?我讨厌丢失我的帐户也许我应该使用代理?是否有比PhantomJS更好的东西用于此?
对不起加载的问题,谢谢!
答案 0 :(得分:0)
我认为亚马逊也通过图像发送cookie。所以不要禁用图像加载。确保在完全加载页面后才从一个页面移动到另一个页面。以下是验证码问题的some suggestions。