【问题标题】:node.io : when the job is done, do it againnode.io :工作完成后,再做一次
【发布时间】:2012-04-25 15:37:09
【问题描述】:

我正在用node.io 构建一个scraper

我要抓取的页面每分钟都有新内容。我想每分钟一次又一次地运行我的工作。 (好吧,我可以用 bash 脚本来做到这一点,但我想留在 javascript 中) 这是一项基本工作:

var nodeio = require('node.io'), options = {timeout: 10};

exports.job = new nodeio.Job(options, {
    input: ['hello', 'foobar', 'weather'],
    run: function (keyword) {
        this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) {
            var results = $('#resultStats').text.toLowerCase();
            this.emit(keyword + ' has ' + results);
        });
    }
});

我怎么能这样做?我是node.js的初学者,我在工作中尝试了setInterval(:没有成功。

【问题讨论】:

  • 使用 while(true) 和 setTimeout(60000) 怎么样
  • @user1291492 : 工作永远不会开始
  • 那么您是否测试过您的代码?我看到 node.io 网站上有很多教程。也许这就是开始的地方

标签: node.js web-scraping node.io


【解决方案1】:

试试这个(使用“node<myfile.js>”而不是“node.io<myfile.js>”运行):

var nodeio = require('node.io'), options = {timeout: 10};
var job = {
    input: ['hello', 'foobar', 'weather'],
    run: function (keyword) {
        this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) {
        var results = 'test';//$('#resultStats').text.toLowerCase();
        this.emit(keyword + ' has ' + results);
      });
    }
};

setInterval(function(){
    nodeio.start(new nodeio.Job(options, job), options, function(){});
}, 5000);

您遇到的问题是 node.io 中的以下代码块,当您在运行作业时不提供回调时退出节点:

//Default behaviour is to exit once the job is complete
callback = callback || function (err) {
    if (err) {
        utils.status.error(err);
    }
    process.exit();
};

【讨论】:

    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多