前言:
我的需求是:用户注册后,自动给他生成一张带有他个人信息的图片
找了好几种方法,有不成功的,有样式错乱的,最后终于找到了这个,phantomjs。
phantomjs是一个无界面浏览器
正文:
1,安装phantomjs
phantomjs网址:http://phantomjs.org/download.html
服务器是windows环境的话,将phantomjs.exe的目标路径添加到环境变量里面,后续使用比较方便
查看版本信息:在cmd里面输入phantomjs --version
2,js代码
var page = require('webpage').create(); system = require('system'); //var url = 'http://yule.club.sohu.com/gifttrade/thread/2m2efbrpfui'; var address; if(system.args.length == 1){ phantom.exit(); }else{ adress = system.args[1]; page.open(adress, function (status){ if (status != "success"){ console.log('FAIL to load the address'); phantom.exit(); } page.evaluate(function(){ //此函数在目标页面执行的,上下文环境非本phantomjs,所以不能用到这个js中其他变量 window.scrollTo(0,10000);//滚动到底部 //window.document.body.scrollTop = document.body.scrollHeight; window.setTimeout(function(){ var plist = document.querySelectorAll("a"); var len = plist.length; while(len) { len--; var el = plist[len]; el.style.border = "1px solid red"; } },5000); }); window.setTimeout(function (){ //在本地生成截图 page.render("json2form.png"); console.log(page.content); phantom.exit(); }, 5000+500); }); }
3,Java代码
package com.newsTest.weixin; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; /** * 类名: DownLoad * 包名: com.newsTest.weixin * 作者: zhouyh * 时间: 2014-9-10 下午04:19:46 * 描述: TODO(这里用一句话描述这个类的作用) */ public class DynamicDownLoad { /** * * 方法名:getSrcContent * 作者:zhouyh * 创建时间:2014-9-10 下午06:57:32 * 描述:根据传入的url,调用phantomjs进行下载,并返回源码信息 * @param url * @return */ public static String getSrcContent(String url){ //windows下phantomjs位置 String path = "D:/phantomjs-1.9.7-windows/"; Runtime rt = Runtime.getRuntime(); Process process = null; try { process = rt.exec(path + "phantomjs.exe D:/phantomjs-1.9.7-windows/test.js " + url.trim()); } catch (IOException e) { // TODO 这里写异常处理的代码 e.printStackTrace(); } InputStream is = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuffer sbf = new StringBuffer(); String tmp = ""; try { while((tmp = br.readLine())!=null){ sbf.append(tmp); } } catch (IOException e) { // TODO 这里写异常处理的代码 e.printStackTrace(); } return sbf.toString(); } /** * 方法名:main * 作者:zhouyh * 创建时间:2014-9-10 下午04:19:46 * 描述:TODO(这里用一句话描述这个方法的作用) * @param args * @throws IOException */ public static void main(String[] args){ // TODO Auto-generated method stub String src = DynamicDownLoad.getSrcContent("http://weixin.sogou.com/gzh?openid=oIWsFt9MmzCvfJgaVxEUevPcuUCg"); System.out.println(src); } }
参考博客:
1,通过phantomjs 进行页面截图 - liminshen - 博客园
https://www.cnblogs.com/samli15999/p/7778105.html
2,Java实现网页截屏功能(基于phantomJs) - HanZongBo - 博客园
http://www.cnblogs.com/han108/p/9216583.html
3,java使用phantomJs抓取动态页面 - kaka0930的博客 - CSDN博客
https://blog.csdn.net/kaka0930/article/details/68941932/
4,基于phantomJs的Java后台网页截图技术 - ontology-fhcj的博客 - CSDN博客
https://blog.csdn.net/ontologyFhcj/article/details/55098590
5,java调用phantomjs实现网页截图 - 简书
https://www.jianshu.com/p/41d5e08af0a7