How can one parse HTML server-side with Meteor?

前端 未结 2 1669
不思量自难忘°
不思量自难忘° 2020-12-13 01:20

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

2条回答
  •  悲&欢浪女
    2020-12-13 01:29

    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

提交回复
热议问题