Node.js Nodejs多个异步请求立即失败
我一直在为Discord编写一个bot,但由于多个异步响应为空,我发现它失败了。它似乎只执行8个请求,而忽略了其他请求 正在运行代码摘录:Node.js Nodejs多个异步请求立即失败,node.js,asynchronous,Node.js,Asynchronous,我一直在为Discord编写一个bot,但由于多个异步响应为空,我发现它失败了。它似乎只执行8个请求,而忽略了其他请求 正在运行代码摘录: /*Required modules npm install request //used because it supports HTTPS npm install cheerio */ var request = require('request'); var cheerio = require('cheerio'); var ids = [
/*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);
});
}
/*所需模块
npm安装请求//已使用,因为它支持HTTPS
npm安装cheerio
*/
var请求=要求(“请求”);
var cheerio=需要('cheerio');
变量ID=[
"16286",
"16296",
"16284",
"15563",
"15964",
"15123",
"15592",
"868",
"15626",
"15627",
"339",
"350"
];
var print=“”;
var计数=0;
对于(var x=0;x
在我的控制台上输出:因此,当我运行代码时,返回的前四个响应将创建一个空的头
变量。结果:
$('.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);
});
请尝试console。记录错误并检查请求失败的原因主机服务器可能会将您的rapid fire 12请求视为拒绝服务或违反速率限制。您需要查看确切的错误,可能还需要查看网络跟踪,以了解失败的确切原因。您可能还想知道您的请求可以按任何顺序返回o在print
变量中获取请求的顺序可能是随机的(这取决于主机服务器的具体行为)。这到底是为什么解决问题?这不会刷新请求,这与您在回答中所做的有些相似,但方式不同。@JFriend00您把我和“刷新”这个词混淆了吗。也许你想说的是,这将序列化请求,使其在飞行中一次只能有一个。你尝试过它吗?是的,刷新意味着。这个async.eachSeries将一次发送一个请求,并且在调用回调之前不会接受另一个请求。是的,我在我的pc上运行了此操作。更新了我的回答u@JFriend00为了改进你的答案,你应该在你的答案中解释所有这些,让人们知道你为什么认为你的答案解决了OP的问题。只需发布代码并告诉人们尝试一些东西(即使它是正确的代码)最好的答案是解释您更改了什么以及它为什么解决了问题。@inuyasha555-仅供参考,如果我严格地将请求序列化为一次只运行一个请求(我的答案中的第二个代码块),它也会起作用。第二个方案还保留了响应的顺序,以与请求的顺序保持一致。
/*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);
});