15

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.

1

2 Answers 2

13

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.

its an npm module so first step install it (inside your project dir) with terminal:

meteor add http
cd .meteor
npm install cheerio

So now the code:

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()      
    }
})

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) } );

I got this as of today/feb 23rd

enter image description here

which the same as on github.com/meteor/meteor

enter image description here

2
  • I really love Meteor, it's just so much fun. Thanks for the excellent answer.
    – Simon
    Commented Feb 23, 2013 at 0:24
  • With Meteor 1.2.1, __meteor_bootstrap__.require('cheerio'); or Meteor.require('cheerio'); doesnt work. Even some other posts suggest Meteor.npmRequire('cheerio') doesnt work. What worked for me is Npm.required('cheerio'), but still answer by @Akshat did help me scrap html response from http package.
    – Jay
    Commented Dec 4, 2015 at 15:13
10

Use cheerio, as Akshat suggests, but I would recommend a different way of using it, as of now, for Meteor 0.8.0.

First, install npm for Meteor:

$ mrt add npm

Then modify packages.json to (of course you can have different version of cheerio, or other node packages as well):

{
  "cheerio": "0.15.0"
}

In server.js (or any other file, in server-side code) start:

var cheerio =  Meteor.require('cheerio');

The use cheerio in a way you like.

Upon running $ meteor it will automatically install cheerio.

1
  • thank you, this is the approach that finally worked! Commented Sep 3, 2014 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.