245940
使用superagent+cheerio实现ndoe爬虫,怎样保证代码同步执行呢,我写的爬虫程序好像有时客户端请求数据会出问题,特别是抓取的数据有大量循环时,我总觉得和node的异步有关系,谁能给个好的方案,就好比下面这个简单demo。谢谢
router.get('/hotCity', function (req, res) {
superagent.get('http://www.daoway.cn/')
.end(function (err, response) {
var $ = cheerio.load(response.text);
var hotCitys = [];
$('.footer dd i').each(function (index, ele) {
hotCitys.push($(ele).text());
});
res.json({
status: 1,
hotCitys: hotCitys
});
})
});