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 = { width: w, height: h };
page.clipRect = { top: 0, left: 0, width: 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 = { width: w, height: h };
page.clipRect = { top: 0, left: 0, width: 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);
  }
});
复制代码

 

posted @   卡卡西村长  阅读(150)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2016-05-27 centos ssh免密码秘钥登录
点击右上角即可分享
微信分享提示