【问题标题】:render page on phantomjs with cron in node使用节点中的 cron 在 phantomjs 上渲染页面
【发布时间】:2018-04-28 20:32:40
【问题描述】:

我需要将页面渲染为 img。我正在使用 phantomjs、childProcess 和 cron。如果你知道另一种方式 - 请写出来。以我的方式,我接下来会这样做,但它第一次只能工作一次。请帮帮我。

这是 cron 代码:

var Cron = require('cron').CronJob;
var i = 0;

var job = new Cron({
  cronTime: "*/10 * * * * *",
  onTick: function() {
    console.log("iteration is ", i);
    var render = require('../tests/initphantom');
    i++;
  }
});

job.start();

phantomjs 代码:

var webPage = require('webpage');
var page = webPage.create();

page.viewportSize = {
  width: 1920,
  height: 5000
};
page.open("http://localhost:3000/", function start(status) {
  console.log("status", status);
  page.render('test1.jpeg', {
    format: 'jpeg',
    quality: '100'
  });
  phantom.exit();
});

并启动代码:

var path = require('path');
var childProcess = require('child_process');
var phantomjs = require('phantomjs');
var binPath = phantomjs.path;


var childArgs = [
  path.join(__dirname, '/phantomTest.js')
]

module.exports.rendr = childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
  if (err) {
    console.log("err in childProcess", err)
  }
});

【问题讨论】:

    标签: javascript node.js cron phantomjs render


    【解决方案1】:

    感谢收看!答案在这里: phantomjs 代码 - 相同,但我已经获得了 cron 代码并且它可以工作:

    var Cron = require('cron').CronJob;
    var i = 0;
    var path = require('path');
    var childProcess = require('child_process');
    var phantomjs = require('phantomjs');
    var binPath = phantomjs.path;
    
    
    var childArgs = [
      path.join(__dirname, '/phantomTest.js')
    ]
    
    
    
    
    var job = new Cron({
      cronTime: "*/10 * * * * *",
      onTick: function() {
        console.log("iteration is ", i);
        childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
          console.log("stderr", stderr);
          console.info("stdout", stdout);
          if (err) {
            console.log("err in childProcess", err)
          }
        });
        i++;
      }
    });
    
    job.start();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多