首先,我们在本地或者服务器中安装PhantomJS -- 下载链接 - http://phantomjs.org/download.html
然后,我们可以按照PhantomJS官网中介绍一样,添加对应脚本, 文件名为:phantom.js
var system = require('system');
var page = require('webpage').create();
var address, output, time;
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7';

page.viewportSize = { width: 1024, height: 768 };

if( system.args.length != 4 ) {
  console.log('Usage: capture.js URL filename');
  phantom.exit(1);
} else {
  address = system.args[1];
  output  = system.args[2];
  time    = system.args[3];
  // console.log(address);
  // console.log(output);

  page.settings.resourceTimeout = time; // Default 5 seconds
  page.settings.javascriptEnabled = false; // Disbale Javascript
  page.open(address, function (status) {
    page.evaluate(function () { document.body.bgColor = 'white'; });

    if (status !== 'success') {
      // console.log('PhantomJS: Unable to load the address: ' + address + " status: " + status + " time: " + time);
      phantom.exit();
    } else {
      // console.log('PhantomJS: Success load the address: '  + address + " status: " + status + " time: " + time);
      window.setTimeout(function () {
        page.render(output);
        phantom.exit();
      }, time);
    }
  });

  page.onError = function(msg, trace) {
    // var msgStack = ['PHANTOM ERROR: ' + msg];
    // if (trace && trace.length) {
    //   msgStack.push('TRACE:');
    //   trace.forEach(function(t) {
    //     msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
    //   });
    // }
    // console.log(msgStack.join('\n'));
    phantom.exit(1);
  };

  page.onResourceTimeout = function(request) {
    // console.log('onResourceTimeout -> Response (#' + request.id + '): ' + JSON.stringify(request));
    phantom.exit(1);
  };

  page.onResourceRequested = function(request) {
    // console.log('onResourceRequested -> Response (#' + request.id + '): ' + JSON.stringify(request));
    // console.log('onResourceRequested -> Response (#' + request.id + ')');
  }
}

命令解释

phantomjs 脚本文件 网站地址 文件生成地址 timeout


如果您使用的是Mac, 安装成功后,可以直接运行如下命令:
pngfile = Tempfile.new(['icmoc_', '.jpg'])
phantomjs phantom.js http://icmoc.com pngfile.path 5000

如果您使用的是Linux, 安装成功后,可以直接运行如下命令:
pngfile = Tempfile.new(['icmoc_', '.jpg'])
timeout -k 120 60 phantomjs --ssl-protocol=any --ignore-ssl-errors=yes phantom.js http://icmoc.com pngfile.path 5000