mac os x 环境 本人亲自测试ok:
1 安装phantomjs
brew install phantomjs
2 建立js文件
/**
* Created by xiehonghao on 16-7-24.
*/
//codes.js
system = require('system')
address = system.args[1];//获得命令行第二个参数 接下来会用到
//console.log('Loading a web page');
var page = require('webpage').create();
var url = address;
//console.log(url);
page.open(url, function (status) {
//Page is loaded!
if (status !== 'success') {
console.log('Unable to post!');
} else {
//console.log(page.content);
//var title = page.evaluate(function() {
// return document.title;//示范下如何使用页面的jsapi去操作页面的 www.oicqzone.com
// });
//console.log(title);
console.log(page.content);
}
phantom.exit();
});
3 测试文件
/**
* Created by xiehonghao on 16-7-24.
*/
public class HttpUtils {
public static String getAjaxCotnent(String url) throws IOException {
Runtime rt = Runtime.getRuntime();
//phantomjs 和codes.js的路径之间有个空格 本代码只是测试用的绝对路径
Process p = rt.exec("phantomjs /Users/hackdream/xiehonghao/gebilaowang/src/main/java/org/gebilaowang/utils/codes.js "+url);
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sbf = new StringBuffer();
String tmp = "";
while((tmp = br.readLine())!=null){
sbf.append(tmp);
}
//System.out.println(sbf.toString());
return sbf.toString();
}
public static void main(String[] args) throws IOException {
String content = getAjaxCotnent("http://cq.qq.com/baoliao/detail.htm?294064");
assert content != null;
}
}
下载最新的chromedriver,然后放到一个目录下,再手动置顶chromedriver位置进行调用java代码如下System.setProperty("webdriver.chrome.driver","D:\\software\\chromedriver_win32\\chromedriver.exe");Python应该是类似
有几个方法可以供你调用。 建议你直接使用第二个就行,当然,要是有其它需求也可以调用其它方法
//关闭当前焦点所在的窗口
webDriver.Close()
//调用dispose方法
webDriver.Quit()
//关闭所有窗口,并且安全关闭session
webDriver.Dispose()