我一直在为Discord编写机器人,由于多个异步响应为空,我发现它已失败。它似乎只做了8个请求而忽略了其他请求。
从代码中摘录:
/*Required modules
npm install request //used because it supports HTTPS
npm install cheerio
*/
var request = require('request');
var cheerio = require('cheerio');
var ids = [
"16286",
"16296",
"16284",
"15563",
"15964",
"15123",
"15592",
"868",
"15626",
"15627",
"339",
"350"
];
var print = "";
var count = 0;
for(var x=0;x<ids.length;x++) {
request({uri: "https://www.futbin.com" + "/17/player/"+ids[x], gzip: true}, function(error, response, full) {
let $ = cheerio.load(full);
var bins = $('.lowest_bin_next_tr');
var header = $('.player_header').text().trim().split("-");
console.log(header);
if(header != '') {
print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n";
print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n";
print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n";
}
if(++count == ids.length)
callback(print);
});
}
4个请求立即失败,因为有12个ID。为什么这只能满足8个请求?
答案 0 :(得分:1)
'strict mode';
var async = require('async'),
request = require('request');
var cheerio = require('cheerio');
var ids = [
"16286",
"16296",
"16284",
"15563",
"15964",
"15123",
"15592",
"868",
"15626",
"15627",
"339",
"350"
];
var print = "";
async.eachSeries(ids , idsIteration , finishIteration);
function idsIteration(id , callBack){
processId(id , callBack);
}
function processId(id , cb){
request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}, function(error, response, full) {
if(error){
console.log('there was error entertainting request :' + error);
cb(error);
}
else {
var c = cheerio.load(full);
var bins = c('.lowest_bin_next_tr');
var header = c('.player_header').text().trim().split("-");
console.log(header);
if(header != '') {
print += header[1].trim() + ", " + header[0] + "OVR " + c('.pcdisplay-pos').text().trim() + "\r\n";
print += "**PS4:** " + c('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + c('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n";
print += "**XBOX:** " + c('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + c('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n";
}
cb();
}
});
}
function finishIteration(){
console.log('all ids processed');
}
答案 1 :(得分:0)
因此,当我运行您的代码时,返回的前四个响应会创建一个空的header
变量。结果:
$('.player_header').text().trim().split("-");
是一个空字符串。
但是,每次我运行它时,我会得到一个空的不同的四个。当你发送12个快速火灾请求时,我认为这是某种服务器端问题。
在测试中,如果我将请求减慢到每秒一个请求,则所有12个报告数据。这绝对是一些服务器端问题,它不喜欢一次收到那么多请求。
这是一个测试代码示例,它将请求速度降低到每秒一个,效果很好。我不是建议这是生产代码,而是想向您展示每秒发出一次请求成功的代码:
/*Required modules
npm install request //used because it supports HTTPS
npm install cheerio
*/
var request = require('request');
var cheerio = require('cheerio');
var ids = [
"16286",
"16296",
"16284",
"15563",
"15964",
"15123",
"15592",
"868",
"15626",
"15627",
"339",
"350"
];
var print = "";
var count = 0;
ids.forEach(function(id, x) {
setTimeout(() => {
request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}, function(error, response, full) {
if (error) {
console.log(error);
} else {
let $ = cheerio.load(full);
var bins = $('.lowest_bin_next_tr');
console.log(`header data [${id}]`, `"${$('.player_header').text().trim()}"`);
var header = $('.player_header').text().trim().split("-");
if(header != '') {
print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n";
print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n";
print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n";
}
if(++count == ids.length)
callback(print);
}
});
}, x * 1000);
});
function callback(data) {
console.log(data);
}
仅供参考,如果我严格序列化它们(当时在飞行中有一个请求),它也有效:
/*Required modules
npm install request //used because it supports HTTPS
npm install cheerio
*/
var Promise = require('bluebird');
var request = Promise.promisify(require('request'), {multiArgs: true});
var cheerio = require('cheerio');
var ids = [
"16286",
"16296",
"16284",
"15563",
"15964",
"15123",
"15592",
"868",
"15626",
"15627",
"339",
"350"
];
Promise.mapSeries(ids, function(id) {
return request({uri: "https://www.futbin.com" + "/17/player/"+id, gzip: true}).then(function(data) {
var response = data[0];
var full = data[1];
let $ = cheerio.load(full);
var bins = $('.lowest_bin_next_tr');
console.log(`header data [${id}]`, `"${$('.player_header').text().trim()}"`);
var header = $('.player_header').text().trim().split("-");
let print = "";
if(header != '') {
print += header[1].trim() + ", " + header[0] + "OVR " + $('.pcdisplay-pos').text().trim() + "\r\n";
print += "**PS4:** " + $('#pslbin').text().slice(0, -1) + ", " + bins.eq(0).text().split("d")[1].trim() + ", " + bins.eq(1).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_ps4').text().trim().substring(8, this.length) + "\r\n";
print += "**XBOX:** " + $('#xboxlbin').text().slice(0, -1) + ", " + bins.eq(4).text().split("d")[1].trim() + ", " + bins.eq(5).text().split("d")[1].trim() + " " + $('.lowest_bin_updated_tr_xb1').text().trim().substring(8, this.length) + "\r\n"; "\r\n";
}
return print;
});
}).then(function(results) {
console.log(results);
});