Node.js - Using a callback function with Cheerio

走远了吗. 提交于 2019-12-10 22:43:44

问题


I'm building a scraper in Node, which uses request and cheerio to load in pages and parse them.

It's important that I put a callback only AFTER Request and Cheerio has finished loading the page. I'm trying to use the async extension, but I'm not entirely sure where to put the callback.

request(url, function (err, resp, body) {
    var $;
    if (err) {
        console.log("Error!: " + err + " using " + url);
    } else {
        async.series([
            function (callback) {
                $ = cheerio.load(body);
                callback();
            },
            function (callback) {
               // do stuff with the `$` content here
            }
        ]);
    }
});

I've been reading the cheerio documentation and can't find any examples of callbacks for when the content has been loaded in.

What's the best way to do it? When I throw 50 URLs at the script it starts moving on too early before cheerio has properly loaded in content, and I'm trying to curb any errors with async loading.

I'm totally new to asynchronous programming and callbacks in general so if I'm missing something simple here please let me know.


回答1:


Yes, cheerio.load is synchronous and you don't need any callbacks for it.

request(url, function (err, resp, body) {
  if (err) {
    return console.log("Error!: " + err + " using " + url);
  }
  var $ = cheerio.load(body);
  // do stuff with the `$` content here
});


来源:https://stackoverflow.com/questions/17140589/node-js-using-a-callback-function-with-cheerio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!