采用phantomjs进行网页截图 /** * 采用phantomjs进行网页截图 * @author lizheng * */public class ScreenShot {public static void main(String[] args) { try { long start=System.currentTimeMillis(); String targetUrl="https://www.cnb
/** * 采用phantomjs进行网页截图 * @author lizheng * */ public class ScreenShot { public static void main(String[] args) { try { long start=System.currentTimeMillis(); String targetUrl="https://www.cnblogs.com"; String targetImg=" D:/200.png"; String command = "E:/phantomjs/bin/phantomjs.exe D:/lizheng.js "+targetUrl+targetImg; Process p = Runtime.getRuntime().exec(command); p.waitFor(); System.out.println((System.currentTimeMillis()-start)+" 毫秒"); } catch (Exception e) { e.printStackTrace(); } } }lizheng.js
var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { console.log('Usage: rasterize.js URL filename'); phantom.exit(1); } else { address = system.args[1]; output = system.args[2]; page.viewportSize = { width: 1024, height: 600 }; page.open(address, function (status) { // 通过在页面上执行脚本获取页面的渲染高度 var bb = page.evaluate(function () { return document.getElementsByTagName('html')[0].getBoundingClientRect(); }); // 按照实际页面的高度,设定渲染的宽高 page.clipRect = { top: bb.top, left: bb.left, width: bb.width, height: bb.height }; // 预留一定的渲染时间 window.setTimeout(function () { page.render(output); page.close(); console.log('render ok'); }, 1000); }); }