将基于cheerio的search循环添加到node-simplecrawler

我正在使用node-simplecrawler来爬取一个网站,并且我需要在每个页面的特定div中search某些属性值。

simplecrawler docs为这样的任务build议以下结构:

myCrawler.on("fetchcomplete",function(queueItem,data,res) { var continue = this.wait(); doSomeDiscovery(data,function(foundURLs){ foundURLs.forEach(crawler.queueURL.bind(crawler)); continue(); }); }); 

我尝试过,但不知道究竟在哪里以及如何将基于Cheerio的search代码插入到该结构中。 真的很感激这里的一些帮助。

 var $ = cheerio.load(html); $('div#jsid-post-container').each(function(i, element){ var StuffINeedToFetch = $(this).attr('data-external-id').text; 

实际上没有必要混淆doSomeDiscovery。 解决scheme是直接使用responseBuffer的内容:

 myCrawler.on("fetchcomplete",function(queueItem, responseBuffer){ html = responseBuffer.toString(); var $ = cheerio.load(html); $('div#jsid-post-container').each(function(i, element){ var StuffINeedToFetch = $(this).attr('data-external-id').text; });