gpt4 book ai didi

node.js - 更新 async.each 中的请求参数?

转载 作者:太空宇宙 更新时间:2023-11-04 00:59:54 24 4
gpt4 key购买 nike

我正在尝试修复我的node.js 抓取程序。

他是其中的一部分:

var site = 'http://www.some.com/';
var startPath = '/hambaarst-1.aspx?ipp=35';
var pageNumArray = [2,3,4,5,6,7,8,9,10,11,12];

async.series([
function(callback) {
async.each(pageNumArray, function(page, callback) {
request(site+startPath, function(err, resp, body){
if(!err && resp.statusCode == 200){
var $ = cheerio.load(body);
$('div.Info').find("a").each(function(){
var url = $(this).attr('href');
doctorPageUrlArray.push(url);
});
} else {
console.log(resp.statusCode);
}
startPath = startPath.slice(0,11)+page+startPath.slice(-12); // HERE, I am changing start path.
callback();
});
}, callback);
},

我需要它循环 11 次,每次都更改 startPath 变量:

startPath = startPath.slice(0,11)+page+startPath.slice(-12);

但它只抓取首页 11 次:'/hambaarst-1.aspx?ipp=35'

所以我猜是因为 Node 的异步 startPath 没有更新。

我该如何解决这个问题?

最佳答案

var async        = require('async');
var format = require('request');
var format = require('util').format;

var site = 'http://www.some.com';
var startPath = '/hambaarst-%s.aspx?ipp=35';
var pageNumArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var doctorPageUrlArray = [];

async.each(pageNumArray, function (page, next) {
console.log(format(site + startPath, page));

request(format(site + startPath, page), function (err, res, body) {
if (!err && resp.statusCode === 200) {
var $ = cheerio.load(body);

$('div.Info').find("a").each(function(){
doctorPageUrlArray.push($(this).attr('href'));
});
} else {
console.log(resp.statusCode);
}

next();
});
}, function (err) {
if (err) {
return console.log(err);
}

console.log(doctorPageUrlArray);
});

关于node.js - 更新 async.each 中的请求参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com