我现在有很多网页要爬,这些地址我已经爬下来放在一个数组里,然后我想用for循环抓取这些网页,代码如下:
db_operation.db_getUrl('appsCategories_China', function(results){
for(var i = 0; i < results.length; i++)
{
var item = results[i];
// console.log(item);
request(item.url, function(err, res, content){
if(!err && res.statusCode == 200)
{
$$ = cheerio.load(content);
getAppsIndex('#selectedcontent', 'a', function(index){
console.log(i);
index.push(item.category);
console.log(item.category);
// db_operation.db_addIndex('appsIndex_China', index)
});
}
});
}
}
结果最后两个控制台输出都是数组results的最后一个元素,应该是因为request的异步执行,导致for循环完了,request还没返回。想请问下我是不是不应该用for循环?求指导!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
用循环可以的,坐下修改~~
使用闭包来封装变量
可以通过添加事件监听的形式,直到所有的内容都返回后再显示数组内的内容,具体实现可以自己来或者使用eventproxy这个库,或者使用promise.all来进行获取。eventproxy模块文档,promise的就自己找一下啦。下面是可能的实现方法:
你可以使用async库的queue方法,很方便,真的。