刮Meteor.js
我可以用meteor.js刮? 刚刚发现了与request
相结合的极好的cheerio
。 我可以使用这些meteor,还是有类似的东西?
你有没有一个工作的例子?
当然! 很难想象meteor不能做什么! 首先你需要一些东西来处理远程http请求。 在你的meteor目录下的terminal中运行meteor add http
来添加Meteor.http
包,还有npm install cheerio
(看看另外一个关于如何安装npm模块的问题,看看npm install cheerio
里安装外部npm模块。
这里有一个例子可以帮助你一点,它会刮目前的时间 。
服务器js
require = __meteor_bootstrap__.require; //to use npm require must be exposed. var cheerio = require('cheerio'); Meteor.methods({ getTime: function () { result = Meteor.http.get("http://www.timeanddate.com/worldclock/city.html?n=136"); $ = cheerio.load(result.content); CurrentTime = $('#ct').html(); return CurrentTime; } });
客户端脚本:
Meteor.call("getTime", function(error, result) { alert("The current time is " + result); });
我希望这是有帮助的。 其中Cheerio还有其他节点框架,如node.io
在这个项目中使用下面的代码来刮掉tweetstorm:
if (Meteor.isClient) { Meteor.call('getTweets', function (error, result) { if (error) { console.log("error", error); }; Session.set("tweets", result); }); Template.tweets.helpers({ rant: function () { return Session.get("tweets"); } }); }
服务器端
if (Meteor.isServer) { Meteor.startup(function () { var cheerio = Meteor.npmRequire('cheerio'); Meteor.methods({ getTweets: function () { result = Meteor.http.get("https://twitter.com/Royal_Arse/status/538330380273979393"); $ = cheerio.load(result.content); var body = $('#stream-items-id > li:nth-child(n) > div > div > p').text(); return body; }, }) }); }
你可以看看http://casperjs.org/这是非常有用的。 你也可以做截图,自动化testing等
现在你应该使用meteorhacks npm包https://github.com/meteorhacks/npm,并要求:
var cheerio = Meteor.npmRequire('cherio');
为我工作:)