因此,Meteor 中的基本服务器 JS scraper 。
这个模式有点简单。脚本找到某些链接,然后从中加载内容并将内容存储在变量中。
在循环内加载 Cheerio 时,脚本不断崩溃。 陷阱在哪里?为此目的的最佳实现是什么?
Meteor.methods({
loadPage: function () {
result = Meteor.http.get("http://url.com");
$ = cheerio.load(result.content);
$('.class').each(function(i,elem){
var link = $(this).attr('href');
var title = $(this).text();
var $ = cheerio.load(Meteor.http.get(link).content);
var postContent = $('.classOnLoadedPage');
Images.insert(
{
link: link,
title: title,
postContent: postContent
});
});
}
});
最佳答案
我今天遇到了完全相同的问题。原来是cheerio本身的问题。相当旧的版本有这个错误。您必须使用较新的版本才能使用。
atmospherejs 中下载次数最多的 Cheerio 包 mrt:cheerio
包含 cheerio 0.12.3
,而 npm 中当前版本是 cheerio 0.19.0
添加 rclai89:cheerio
而不是 mrt:cheerio
,它将提供 cheerio 0.18.0
,并且使用此版本可以在循环内加载完美。
关于node.js - 在每个循环内调用cheerio.load,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242031/