一个人如何用Meteor解析HTML服务器端? [英] How can one parse HTML server-side with Meteor?
问题描述
我希望能够将链接从我使用Meteor.http方法获取的HTML页面中抓取出来.在服务器端使用jQuery是理想的选择,但我认为这行不通.
I want to be able to scrape links out of an HTML page that I am fetching with the Meteor.http method. Would be ideal to use jQuery on the server-side but I don't think this works.
推荐答案
考虑使用 cheerio 像jquery,但更多用于抓取.我曾经尝试回答这个问题,所以我希望这次能做得更好.
Consider using cheerio its just like jquery but more for scraping. I have tried to answer this before so I hope I do a better job this time.
有一个npm模块,因此第一步需要在终端上安装它(在项目目录中):
its an npm module so first step install it (inside your project dir) with terminal:
meteor add http
cd .meteor
npm install cheerio
现在,代码:
您需要在服务器js/或等效版本中使用它
You need to use this in your server js/or equivalent
var cheerio = __meteor_bootstrap__.require('cheerio');
Meteor.methods({
last_action: function() {
$ = cheerio.load(Meteor.http.get("https://github.com/meteor/meteor").content);
return $('.commit-title').text().trim()
}
})
如果从客户端js运行此命令,则会在流星github分支上看到最后一个动作:
If you run this from your client side js, you will see the last action on meteors github branch:
Meteor.call("last_action",function(err,result){ console.log(result) } );
我从今天/2月23日起得到了这个
I got this as of today/feb 23rd
与github.com/meteor/meteor上的相同
which the same as on github.com/meteor/meteor
这篇关于一个人如何用Meteor解析HTML服务器端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!