zoukankan      html  css  js  c++  java
  • centos7上安装phantomjs并对页面截屏

    安装
    wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
    yum install bzip2
    tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
    mv phantomjs-2.1.1-linux-x86_64 /usr/local/src/phantomjs
    ln -sf /usr/local/src/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
    yum install -y fontconfig freetype2
    yum install -y bitmap-fonts bitmap-fonts-cjk
    yum groupinstall "fonts" -y
    fc-cache
    phantomjs -v

    测试1

    创建一个文件:hello.js

    console.log('Hello, world!');
    phantom.exit();

    运行:phantomjs hello.js

    输出:Hello, world!

    测试2

    创建一个文件:example.js

    var page = require('webpage').create();
    page.open('http://example.com', function(status) {
      console.log("Status: " + status);
      if(status === "success") {
        page.render('./example.png');
      }
      phantom.exit();
    });

    运行:phantomjs example.js

    输出图片文件:./example.png

    测试3

    默认的截图尺寸是400*300,这个太小了,可以指定浏览器可视区域尺寸。

    var page = require('webpage').create();
    var w = 1920;
    var h = 792;
    page.viewportSize = {  w, height: h };
    page.clipRect = { top: 0, left: 0,  w, height: h };
    page.open('http://example.com/', function(status) {
      console.log("Status: " + status);
      if(status === "success") {
        page.render('./example.png');
      }
      phantom.exit();
    });

    测试4

    对于异步加载的页面,需要等会待会才能截图。

    var page = require('webpage').create();
    var w = 1920;
    var h = 792;
    page.viewportSize = {  w, height: h };
    page.clipRect = { top: 0, left: 0,  w, height: h };
    page.open('http://example.com', function() {
      console.log("Status: " + status);
      if(status === "success") {
        window.setTimeout(function () {
            page.render('./example.png');
            phantom.exit();
        }, 3000);
      } else {
        console.log('Unable to load the address!');
        phantom.exit(1);
      }
    });

    作者:Lave Zhang
    出处:http://www.cnblogs.com/lavezhang/
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    新安装的Apache和php,测试可以解析phpinfo,但是无法打开drupal网站
    Drupal7安装注意事项
    drupal7 为视图添加 过滤标准 内容类型
    Drupal网站报错:PDOException: in lock_may_be_available()
    窗口聚合函数与分组聚合函数的异同
    Linux环境下段错误的产生原因及调试方法小结(转)
    gzip 所使用压缩算法的基本原理(选摘)
    InfluxDB使用纪录
    子网掩码解释(转)
    列存的压缩原理学习
  • 原文地址:https://www.cnblogs.com/lavezhang/p/14816545.html
Copyright © 2011-2022 走看看