문제

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.

도움이 되었습니까?

해결책

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
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top