综合技术

Node.js – Using a callback function with Cheerio

微信扫一扫,分享到朋友圈

Node.js – Using a callback function with Cheerio
0

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.

Problem courtesy of: Jascination

阅读原文...

微信扫一扫,分享到朋友圈

Node.js – Using a callback function with Cheerio
0
Node.js Recipes

Move your data from AWS S3 to Azure Storage using AzCopy

上一篇

Source: Google Home Hub will be rebranded as Google Nest Hub

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

Node.js – Using a callback function with Cheerio

长按储存图像,分享给朋友