案例是phantomjs来截取百度网页特定区域内的内容,java 代码通过Runtime.getRuntime().exec()来实现命令行 首先需要下载phantomJSjs代码:var page=require('webpage').create();var address='https://www.baidu.com/'
首先需要下载phantomJS
js代码:
var page=require('webpage').create();
var address='https://www.baidu.com/'; // 设置url
var output='kiban.png'; // 设置保存文件名
page.viewportSize={width:1024,height:800}; // 设置查看页面的分辨率
page.open(address,function(status){
if(status!=='success'){
console.log('Unabletoloadtheaddress!');
phantom.exit();
}else{
window.setTimeout(function(){
page.clipRect={top:170, left:10, height: 330, width: 980}; // 设置页边距,从而获取想要的图片,需要慢慢调整
page.render(output); // 保存图片
phantom.exit();
},20000);
}
});
脚本代码testOne:
F:
cd F:\\phantomjsWork
F:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe kibanaTwo.js
java代码:
public class TestOne {
public static void main(String[] args) {
String path = "F:\\TestOne.bat";
Runtime run = Runtime.getRuntime();
try {
Process process = run.exec("cmd.exe /k start " + path);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
