CNode

使用superagent+cheerio实现ndoe爬虫,怎样保证代码同步执行呢,我写的爬虫程序有时客户端请求数据会出问题。求解决方案

问答
Ttfx919发布于9 年前最后回复9 年前2 回复4586 浏览0 收藏

使用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)

G
godghdai#1·9 年前
B
blackmatch#2·9 年前

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

参与回复
登录后即可参与回复。登录