PhantomJS is a server-side JavaScript API based on WebKit. It fully supports the web without browser support. It supports various Web standards in a fast and native way: DOM processing, CSS selectors, JSON, Canvas, and SVG. PhantomJS can be used for page automation, network monitoring, page screenshots, and interface free testing.
start
1. Download phantomjs
stay http://phantomjs.org/download.html Download phantomjs from. I operate under the window system here, so I download the first window version here. Sometimes this website can't be downloaded. I uploaded an attachment below. If the official website can be downloaded, please download it on the official website.
The address where I upload the attachment: https://download.csdn.net/download/liguoqingxjxcc/10591612
2. After downloading, I put it on disk E and unzip it to the current folder. My path here is E:\phantomjs-2.1.1-windows. Add the path E:\phantomjs-2.1.1-windows\bin to the environment variable path. Using phantomjs --version on the cmd command line to see the version number indicates that the setting is successful.
3. In the cmd command, use phantomjs E:/phantomjs-2.1.1-windows/examples/hello.js to see Hello, world! To indicate that it can be used.
4. If we want to use it in java, just execute the cmd command through Process. Please see the following example
public static void main(String[] args){ String url = "http://news.baidu.com"; String saveImgPath = "C:\\Users\\Administrator\\Desktop\\Baidu News.png"; String path = "E:/phantomjs-2.1.1-windows/"; Runtime rt = Runtime.getRuntime(); try { Process p = rt.exec(path + "/bin/phantomjs.exe "+ path +"examples/rasterize.js " + url.trim() + " " + saveImgPath ); p.waitFor(); } catch (Exception e) { e.printStackTrace(); } }
rasterize.js
"use strict"; var page = require('webpage').create(), system = require('system'), address, output, size; address = system.args[1];//Address for screenshot output = system.args[2];//Screenshot save location page.viewportSize = { width: 1360, height: 600 }; page.open(address, function (status) { if (system.args.length > 3 ) {//Width and height of Screenshot console.log("three param!"); size = system.args[3].split('*'); if (size.length === 2) { page.viewportSize = { width: size[0], height: size[1]}; page.clipRect = { top: 0, left: 0, width: size[0], height: size[1] };//Where to start the screenshot and end it } }else { // Get the rendering height of the page by executing a script on the page var bb = page.evaluate(function () { return document.getElementsByTagName('html')[0].getBoundingClientRect(); }); // Set the width and height of the rendering according to the height of the actual page page.viewportSize = { width: bb.width, height: bb.height}; page.clipRect = { top: bb.top, left: bb.left, width: bb.width, height: bb.height }; } if (system.args.length > 4) {//Screenshot ratio console.log("four param!bili:" + system.args[4]); page.zoomFactor = system.args[4]; } if (status !== 'success') { console.log('Unable to load the address!'); phantom.exit(1); } else { window.setTimeout(function () { page.render(output); phantom.exit(); }, 5000); } });
End