JVG*_*JVG 2 asynchronous node.js cheerio
我在Node中构建一个scraper,它使用request和cheerio来加载页面并解析它们.
重要的是我只在请求和Cheerio完成加载页面后才进行回调.我正在尝试使用async扩展,但我不完全确定在哪里放回调.
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
}
]);
}
});
Run Code Online (Sandbox Code Playgroud)
我一直在阅读,cheerio documentation并且无法找到任何内容加载时的回调示例.
最好的方法是什么?当我在脚本上抛出50个URL时,它会在cheerio正确加载内容之前过早地开始移动,而我正试图通过异步加载来控制任何错误.
我对异步编程和回调都很陌生,所以如果我在这里缺少一些简单的东西请告诉我.
是的,cheerio.load是同步的,你不需要任何回调.
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
});
Run Code Online (Sandbox Code Playgroud)