【发布时间】:2015-09-23 08:59:26
【问题描述】:
所以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
});
});
}
});
【问题讨论】:
-
你得到什么错误?
-
@BidhanA Exception while invoking method 'loadPage' TypeError: undefined is not a function and points to "var link ..." inside loop, 但在我删除 cherio load inside loop 后一切正常。
-
你
requirecheerio 正确吗? -
@BidhanA 是的)如果不在每个循环内部调用它,它就可以正常工作。
-
尝试在
$ = cheerio.load(result.content);正下方添加console.log($)以查看$ 是否有任何内容。
标签: node.js meteor web-scraping cheerio