Is it possible to run PhantomJS from node.js as a command line argument

后端 未结 3 810
挽巷
挽巷 2020-12-04 18:53

I was recently going to test out running phantomJS from python as a commandline argument, I haven\'t got round to it yet but have seen examples. Because PhantomJS is run fro

相关标签:
3条回答
  • 2020-12-04 19:16

    I suppose you can make a simple script for Node.js to run; in that script phantomjs script will be run as a child process. You can see the working example (and links for some documentation) in this answer. I suppose this discussion might be helpful for you as well.

    0 讨论(0)
  • 2020-12-04 19:21

    From NPM: https://npmjs.org/package/phantomjs

    var path = require('path')
    var childProcess = require('child_process')
    var phantomjs = require('phantomjs')
    var binPath = phantomjs.path
    
    var childArgs = [
      path.join(__dirname, 'phantomjs-script.js'),
      'some other argument (passed to phantomjs script)'
    ]
    
    childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
      // handle results
    })
    
    0 讨论(0)
  • 2020-12-04 19:27

    As an alternative to Donald Derek's answer, you can use the spawn function. It will allow you to read the child process's output as soon as it's produced rather than the output being buffered and returned to you all at once.

    You can read more about it here.

    An example from the documentation.

    var spawn = require('child_process').spawn,
        ls    = spawn('ls', ['-lh', '/usr']);
    
    ls.stdout.on('data', function (data) {
      console.log('stdout: ' + data);
    });
    
    ls.stderr.on('data', function (data) {
      console.log('stderr: ' + data);
    });
    
    ls.on('close', function (code) {
      console.log('child process exited with code ' + code);
    });
    
    0 讨论(0)
提交回复
热议问题