如何使用 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屋!