phantomJS截取图片

phantomJS主要应用于网络爬虫,可以获取网页的内容及截图,这里简单介绍下截图

下载phantomJS应用

官网下载

http://phantomjs.org/download.html

这里面有windows linux 等应用

我这边先现在windows应用

解压后是这样的

运行

可以运行,直接上代码

java代码

public static void main(String[] args) throws IOException {
		System.out.println("开始");
        String BLANK = "  ";
        Process process = Runtime.getRuntime().exec(
                "D:/Program Files/phantomjs-2.1.1-windows/bin/phantomjs.exe" + BLANK //你的phantomjs.exe路径
                + "C:/Users/Administrator/Desktop/test1.js" + BLANK //就是上文中那段javascript脚本的存放路径
                + "https://blog.csdn.net/nanyanglu/article/details/52671044#comments" + BLANK //你的目标url地址
               +"C:/Users/Administrator/Desktop/cnblogs1111.png");//你的图片输出路径

        InputStream inputStream = process.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String tmp = "";
        System.out.println(reader.readLine());
       
            if (reader != null) {
                reader.close();
            }
            if (process != null) {
                process.destroy();
                process = null;
            
            System.out.println("渲染成功...");
        }
    }

js代码

var page = require('webpage').create(),
    system = require('system'),
    address, output, size;
if (system.args.length < 3 || system.args.length > 5) {
    console.log('Usage: rasterize.js URL filename');
    phantom.exit(1);
} else {
    address = system.args[1];
    output = system.args[2];
    page.viewportSize = { width: 1024, height: 1000 };
    page.open(address, function (status) {
      // 通过在页面上执行脚本获取页面的渲染高度
      var bb = page.evaluate(function () { 
        return document.getElementsByTagName('html')[0].getBoundingClientRect(); 
      });
      // 按照实际页面的高度,设定渲染的宽高
      page.clipRect = {
        top:    bb.top,
        left:   bb.left,
        width:  bb.width,
        height: bb.height
      };
      // 预留一定的渲染时间
      window.setTimeout(function () {
        page.render(output);
        page.close();
        console.log('render ok');
      }, 10000);
    });
}

这样就可以把整个页面截取下来。

在linux下运行截取网页

可以直接在linux上下载,根据自己系统是32位还是64位

 wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

压缩包下载成功了,解压

tar -xjf phantomjs-2.1.1-linux-x86_64.tar.bz2

验证是否可以运行

如果运行的 phantomjs -v 出现以下错误:

bin/phantomjs: error while loading shared libraries: libfontconfig.so.1:
 cannot open shared object file: No such file or directorybin/phantomjs: error while loading 
shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

请在下面相对应的linux版本 运行以下命令
ubuntu
sudo apt-get install libfontconfig

centos
yum install libXext libXrender fontconfig libfontconfig.so.1

运行通过,将phantomjs 设置为全局的命令。

在截取图片时值显示图片,不显示文字(包括中文和英文)

linux中不显示文字即没有安装相应的字体,我们安装字体即可:

 

在centos中执行:yum install bitmap-fonts bitmap-fonts-cjk

 

在ubuntu中执行:sudo apt-get install xfonts-wqy

 

执行上述命令即ok

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值