【问题标题】:Scraping with Meteor.js使用 Meteor.js 进行抓取
【发布时间】:2013-01-25 02:28:43
【问题描述】:

我可以用meteor.js 刮吗?刚刚发现cheeriorequest 结合使用效果很好。我可以将这些与流星一起使用,还是有类似的东西?

你有一个可行的例子吗?

【问题讨论】:

标签: node.js web-scraping meteor cheerio


【解决方案1】:

this project 中使用以下代码来抓取推文风暴:

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

    })

  });
}

【讨论】:

    【解决方案2】:

    现在你应该使用meteorhacks npm 包https://github.com/meteorhacks/npm 并要求:

    var cheerio = Meteor.npmRequire('cherio');
    

    为我工作:)

    【讨论】:

      【解决方案3】:

      您可以查看http://casperjs.org/,非常有用。还可以做截图、自动化测试等……

      【讨论】:

        【解决方案4】:

        当然!很难想象流星不能做什么!首先,您需要一些东西来处理远程 http 请求。在终端的流星目录中运行meteor add http 以添加Meteor.http 包,还有npm install cheerio(查看another SO question on how to install npm modules 以了解安装外部npm 模块的确切位置。

        这是一个可能对您有所帮助的示例,它会抓取 current time

        服务器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

        【讨论】:

        • 你有什么模拟JS刮的推荐吗?你的例子看起来像它只做 HTML
        • @FullStack 如果您需要进行更高级的抓取,请查看使用 phantomjs。
        猜你喜欢
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多