我使用 node.js 从 10 个不同的网站收集消息和通知,然后将它们发送到我的电子邮件帐户(类似于每日报告)。我已经为 5 个不同的网站完成了这项工作,但现在我遇到了亚马逊的问题。
我需要对亚马逊做的是使用我的凭据登录,然后解析一些数据。
经过几次实施和不同的方法,我终于意识到亚马逊拒绝我登录,因为不支持 cookie,但事实并非如此,我的 node.js 脚本支持 cookie 我正在使用 cookie 登录其他网站。
作为证明,我使用 node.js 打开了 Amazon 登录页面,然后将响应保存到 .html 文件。当我打开这个保存的 html 文件时,我可以看到亚马逊要求我启用 cookie。这是屏幕截图:
这是我的 node.js 脚本:
var request = require("request");
request = request.defaults({jar: true});//Send cookies on every subsequent requests. This is really important
var requestHeaders = new DRequestHeaders();
var cheerio = require('cheerio');
var fs = require('fs');
var user = new DUser('test','test');
//First open start site: http://amazon.com
request.get({headers:requestHeaders.getHeaders(), uri: user.getUrl('startSeite'), followRedirect: true, maxRedirects: 10,}, function(err, res, body) {
if(err) {
console.log('GET request failed here is error');
console.log(res);
}
//Get login link
$=cheerio.load(body);
user.setNewUrl('login',$('#nav-link-yourAccount').attr('href'));
console.log(user.getUrl('login'));
//Go to the login link
request.get({headers:requestHeaders.getHeaders(), uri: user.getUrl('login'), followRedirect: true, maxRedirects: 10,}, function(err, res, body) {
if(err) {
console.log('GET request failed here is error');
console.log(res);
}
//Now we are at login page. We want to prepare for form submittion
//Get hidden input fields
$ = cheerio.load(body);
var inputsFields = $('#ap_signin_form :input');//Get all inputs
user.setNewUrl('form-submit',$('#ap_signin_form').attr('action'));
var formSubmitData = {};
inputsFields.each(function() {
formSubmitData[this.attribs['name']]=this.attribs['value'];//Get attribute name and value
});
//Set username and password, and now everything is ready for form submission
formSubmitData['email'] = user.getUsername();
formSubmitData['password'] = user.getPassword();
//This is just for demonstration. I save response from amazon so I can clearly see the message that cookies are not supported
fs.writeFile("amazon.html", body, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
});
当我打印响应对象并找到原始请求时,我可以清楚地看到 node.js 确实发送了一些 cookie,但为什么亚马逊拒绝它们?
GET /ap/signin/278-4773600-2469922?_encoding=in HTTP/1.1\r\n
Accept: text/html\r\n
Accept-Language: en-US\r\n
Cache-Control: max-age=0\r\n
Connection: keep-alive\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36\r\n
Cookie: x-wl-uid=1VIRmGEpmmFvZeJXhczJzp7Uv4qmAA6yIt40QrU9OMMLth6j0vSM0j7lKYDYo=; session-id-time=2082751l; session-id=944-4770-24922\r\n
content-type: application/x-www-form-urlencoded\r\n
host: www.amazon.com