java phantomjs
Posted sobigdata
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java phantomjs相关的知识,希望对你有一定的参考价值。
1:下载:phantomjs http://phantomjs.org/download.html
2:JAVA代码
public void HttpunitlStr(String url) { HTML=""; String jsPath = "C:\phantomjs\examples\myjs.js"; String exePath = "C:\phantomjs\bin\phantomjs.exe"; System.out.println(jsPath); System.out.println(exePath); Runtime rt = Runtime.getRuntime(); Process p; try { p = rt.exec(exePath + " " + jsPath + " " + 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); } HTML=sbf.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
3:js代码
var page = require(‘webpage‘).create(), system = require(‘system‘), t, address; page.settings.loadImages = false; //为了提升加载速度,不加载图片 page.settings.resourceTimeout = 10000;//超过10秒放弃加载 //此处是用来设置截图的参数。不截图没啥用 page.viewportSize = { width: 1280, height: 800 }; block_urls = [‘google.com‘];//为了提升速度,屏蔽一些需要时间长的。比如百度广告 page.onResourceRequested = function(requestData, request){ for(url in block_urls) { if(requestData.url.indexOf(block_urls[url]) !== -1) { request.abort(); return; } } } t = Date.now();//看看加载需要多久。 address = system.args[1]; page.open(address, function(status) { if (status !== ‘success‘) { console.log(‘FAIL to load the address‘); } else { t = Date.now() - t; console.log(‘Loading time ‘ + t + ‘ msec‘); console.log(page.content); setTimeout(function(){ phantom.exit(); }, 6000); } phantom.exit(); });
以上是关于java phantomjs的主要内容,如果未能解决你的问题,请参考以下文章
selenium phantomjs java×××面浏览器环境搭建
Selenium java + PhantomJS - phantomjs://platform/console++.js:263错误
如何获取嵌入在 PhantomJS 运行的 JS 的 HTML 页面结果中的 JSON 对象并将它们传递给 java 代码?