我正试图从amazon.com提取某产品的货运价格。亚马逊在页面的左上方显示了一个名为“ Deliver to XXX Country”的div,以了解到该国家的货运价格。我正在选择加拿大的国家/地区,据我所说,亚马逊在浏览器中创建了一个cookie。在检查了一下Chrome设置后,有一个名为csm-hit
的cookie,其值等于tb:s-SSR8495MRZX4TASZBPFJ|1549741920304&t:1549741921521&adb:adblk_yes
。我90%肯定是保留“发送到加拿大”信息的cookie。
要将其应用于js代码,我打算将货物价格运到加拿大:
const fs = require('fs'),
xlsx = require('node-xlsx');
request = require('request-promise'),
cheerio = require('cheerio'),
constantUrl = 'http://www.amazon.com/gp/product/';
var asinList = [],
results = [],
tough = require('tough-cookie');
let cookie = new tough.Cookie({
key: "csm-hit",
value: "tb:s-SSR8495MRZX4TASZBPFJ|1549741920304&t:1549741921521&adb:adblk_yes",
domain: 'amazon.com',
httpOnly: true,
maxAge: 31536000
});
var cookiejar = request.jar();
cookiejar.setCookie(cookie.toString(), 'https://amazon.com');
var opts = {
uri: "",
headers: {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
},
gzip: true, //Added to fix the issue,
_disableCookies: false,
_jar: cookiejar
};
//Reading Excel File
object = xlsx.parse(__dirname + '/example.xlsx');
//Assigning ASIN codes in Excel file to asinList array
for (var i = 1; i < object[0].data.length; i++) {
asinList.push(object[0].data[i][2]);
}
//Requesting to Amazon according to ASIN codes
var price;
var sayac = 0;
asinList.forEach(function(elem) {
opts.uri = constantUrl + elem;
request(opts)
.then(function(html){
var $ = cheerio.load(html);
price = $('#priceblock_ourprice').text();
price = price.trim();
price = price.split('$');
para = parseFloat(price[1]);
shPrice = $("#ourprice_shippingmessage span.a-size-base.a-color-secondary").text()
console.log(elem);
console.log(para);
console.log(shPrice);
console.log("______________");
console.log("\n");
results.push({
"asin": elem,
"price": para,
"shippingPrice": shPrice
});
if (sayac == 23) {
finisher();
}
sayac++;
})
.catch(function(err){
console.log(err);
});
});
function finisher(){
setTimeout(function(){
console.log(results);
console.log("Results length : " + results.length);
console.log("Asin length : " + asinList.length);
},3000);
}
运行此命令后,货运价格无法控制。我在哪里错了?