原文:https://www.jianshu.com/p/8210a17bcdb8html
原文:http://www.javashuo.com/article/p-gegphweo-w.htmljava
PhantomJS是一个基于webkit的JavaScript API。它使用QtWebKit做为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码。任何你能够在基于webkit浏览器作的事情,它都能作到。它不只是个隐形的浏览器,提供了诸如CSS选择器、支持Web标准、DOM操做、JSON、HTML五、Canvas、SVG等,同时也提供了处理文件I/O的操做,从而使你能够向操做系统读写文件等。PhantomJS的用处可谓很是普遍,诸如网络监测、网页截屏、无需浏览器的 Web 测试、页面访问自动化等。git
PhantomJS官方地址:http://phantomjs.org/。
PhantomJS官方API:http://phantomjs.org/api/。
PhantomJS官方示例:http://phantomjs.org/examples/。
PhantomJS GitHub:https://github.com/ariya/phantomjs/
PhantomJS 下载:https://phantomjs.org/download.htmlgithub
下载完成后解压文件,建议为方便使用,单独放在一个文件夹里,如放在D:\workspace\phantomjs里。web
到这里,你已经成功下载安装好PhantomJS了。那么,打开D:\workspace\phantomjs\bin文件夹,双击运行phantomjs.exe,出现以下界面,那么你就能够运行JS代码了。小程序
能够考虑将D:\workspace\phantomjs\bin路径配置到环境变量中.方便往后使用windows
// demo.js var page = require('webpage').create(); phantom.cutputEncoding = 'gbk'; page.open("https://www.jianshu.com", function(status) { if(status === "success") { page.render("jianshu.png"); } else { console.log("Page failed to load."); }; phantom.exit(); });
而后在cmd命令行中, 切换到demo.js全部目录运行demo.js.api
demo.js会将抓取到页面的截图保存到当前页面
如上图所示的 jianshu.png.浏览器
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * @Description:根据网页地址转换成图片 * @Author: admin * @CreateDate: 2018年6月22日 */ public class PhantomTools { private static String tempPath = "D:/temp/img";// 图片保存目录 private static String BLANK = " "; // 下面内容能够在配置文件中配置 private static String binPath = "D:/tooles/phantomjs/phantomjs-2.1.1-windows/bin/phantomjs.exe";// 插件引入地址 private static String jsPath = "D:/tooles/phantomjs/phantomjs-2.1.1-windows/examples/rasterize.js";// js引入地址 // 执行cmd命令 public static String cmd(String imgagePath, String url) { return binPath + BLANK + jsPath + BLANK + url + BLANK + imgagePath; } //关闭命令 public static void close(Process process, BufferedReader bufferedReader) throws IOException { if (bufferedReader != null) { bufferedReader.close(); } if (process != null) { process.destroy(); process = null; } } /** * @param userId * @param url * @throws IOException */ public static void printUrlScreen2jpg(String url) throws IOException{ String imgagePath = tempPath+"/"+System.currentTimeMillis()+".png";//图片路径 //Java中使用Runtime和Process类运行外部程序 Process process = Runtime.getRuntime().exec(cmd(imgagePath,url)); InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String tmp = ""; while ((tmp = reader.readLine()) != null) { close(process,reader); } System.out.println("success"); } public static void main(String[] args) throws IOException { String url = "https://www.baidu.com/";//以百度网站首页为例 PhantomTools.printUrlScreen2jpg(url); } }
负责截图脚本examples文件夹下rasterize.js以下:网络
var page = require('webpage').create(), system = require('system'), address, output, size; if (system.args.length < 3 || system.args.length > 5) { phantom.exit(1); } else { address = system.args[1];//传入url地址 output = system.args[2];//输出图片的地址 page.viewportSize = { width: 800, height: 1800 };//自定义定义宽高 if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") { size = system.args[3].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: system.args[3], orientation: 'portrait', margin: '1cm' }; } else if (system.args.length > 3 && system.args[3].substr(-2) === "px") { size = system.args[3].split('*'); if (size.length === 2) { pageWidth = parseInt(size[0], 10); pageHeight = parseInt(size[1], 10); page.viewportSize = { width: pageWidth, height: pageHeight }; page.clipRect = { top: 0, left: 0, width: pageWidth, height: pageHeight }; } else { console.log("size:", system.args[3]); pageWidth = parseInt(system.args[3], 10); pageHeight = parseInt(pageWidth * 3/4, 10); // it's as good an assumption as any console.log ("pageHeight:",pageHeight); page.viewportSize = { width: pageWidth, height: pageHeight }; } } if (system.args.length > 4) { page.zoomFactor = system.args[4]; } page.open(address, function (status) { if (status !== 'success') { console.log('Unable to load the address!'); phantom.exit(1); } else { window.setTimeout(function () { page.render(output); phantom.exit(); }, 200);//防止图片加载过慢 } }); }