【问题标题】:Node.io scrape job fails the 2nd timeNode.io 抓取作业第二次失败
【发布时间】:2012-10-30 19:00:06
【问题描述】:

我实际上是在尝试即时抓取页面。当您点击此网址时,它会输出抓取作业的结果。第一次一切都很好。我第二次尝试它(通过 job.options.args 传递不同的参数)它甚至不会执行 node.io 作业的 run() 函数。 scrape_result 第二次返回空(我期待一个对象)。

有什么想法吗?如何确保第二次返回新结果?对于我的抓取工作,我几乎完全使用这里的示例#3:https://github.com/chriso/node.io/wiki/Scraping

scraper.js 的摘录(其余如示例 #3:https://github.com/chriso/node.io/wiki/Scraping

run: function() {
    var book = this.options.args[0].book;
    var chapter = this.options.args[0].chapter;

    this.getHtml('http://www.url.com' + book + '/' + chapter + '?lang=eng', function(err, $) {

然后是我的 app.js

var scrip_scraper = require('./scraper.js');

app.get('/verses/:book/:chapter', function (req, res) {
    var params = {
        book: req.param('book'),
        chapter: req.param('chapter')
    }

    scrip_scraper.job.options.args[0] = params;
    //scrip_scraper.job.options.args.push(chapter);
    console.log(scrip_scraper.job.options.args);



    nodeio.start(scrip_scraper, function (err, scrape_result) {

        console.log(scrape_result);
    }, true);

}); //app.get('/verses/:book/:chapter')

【问题讨论】:

  • 我认为为了帮助您,我们需要查看更多您的代码。你是如何创建scrip_scraper 的?我不认为scrip_scraper.job.options.args[0] = params; 正在做你想做的事。
  • @Max 我在上面添加了更多代码。我无法弄清楚将参数传递给我的抓取工作的正确方法。使用 options.args[0] 是我能想到的最好的方法。它第一次很好地工作。第二次,run() 似乎没有执行。

标签: node.js express node.io


【解决方案1】:

您可能会遇到范围界定问题,因为在发出请求时options.args 可能会发生变化。尝试将输入作为函数参数传递给作业,这样它就不能被另一个请求更改。这是一个您可以根据需要进行调整的示例

app.js

var express = require('express')
  , scraper = require('./scraper')
  , app = express();

app.get('/:keyword', function (request, response, next) {
    scraper(request.param('keyword'), function (err, result) {
        if (err) {
            return next(err);
        }
        response.send(result);
    });
});

app.listen(3000);

scraper.js

var nodeio = require('node.io');

module.exports = function (keyword, callback) {
    var job = new nodeio.Job({
        input: [ keyword ]
      , run: function (keyword) {
            //Make the request here..
            this.emit(keyword);
        }
    });
    nodeio.start(job, { silent: true }, callback, true);
};

【讨论】:

  • 效果很好。不太清楚为什么... :) 我尝试实现您的解决方案的一部分,然后首先,它会在每次作业完成后杀死我的节点服务器。但现在效果很好。
  • 问题:我之前使用这个签名来调用 fn nodeio.start(new nodeio.Job({timeout:10, silent: true}, methods, callback, true))。第二个参数是方法,而在您的示例中,第二个参数是配置 obj。那个 API 有变化吗?
猜你喜欢
  • 1970-01-01
  • 2013-12-17
  • 2020-10-02
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多