使用superagent+cheerio实现ndoe爬虫,怎样保证代码同步执行呢,我写的爬虫程序有时客户端请求数据会出问题。求解决方案
发布于 7 年前 作者 tfx919 3970 次浏览 来自 问答

使用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
      });
    })
});
2 回复

我能有什么办法,为了用import我都上babel了。。。。用es7的asycn/await,或者co+promise也可以。

回到顶部