第一步,我们先来个hello world
"use strict";
console.log('Hello, world!');
phantom.exit();
保存为hello.js,打开terminal,进入到hello.js所在文件夹,运行命令:
phantomjs hello.js
terminal输出:
Hello, world!
第二步,保存网页到图片
"use strict";
var page = require( 'webpage' ).create();
page.viewportSize = { width: 1920, height: 1080 };
page.open( "https://www.baidu.com", function ( status ) {
if ( status === "success" ) {
page.render( "baidu.png" );
} else {
console.log( "Page failed to load." );
}
phantom.exit();
} );
以上代码保存为print.js,打开terminal,运行命令:
phantomjs print.js
文件夹多了个baidu.png的图片,这就是保存的网页截图。