在每个循环内调用cheerio.load
所以Meteor的基本服务器JS scrapper。
这种模式很简单。 脚本find某些链接,然后从它们加载内容并将内容存储在variables中。
在循环中加载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本身的问题。 它的老版本有这个错误。 你必须使用较新的版本,然后才能正常工作。
下载最多的在气氛中的cheerio包mrt:cheerio
包裹cheerio 0.12.3
,而在npm中的当前版本是cheerio 0.19.0
添加rclai89:cheerio
而不是mrt:cheerio
,它会提供cheerio 0.18.0
,并且这个版本内的循环加载完美。