1、安装环境
[root@wrx install]# cat /proc/version Linux version 2.6.32-642.13.1.el6.i686 (mockbuild@c1bm.rdu2.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Wed Jan 11 20:21:04 UTC 2017
2、安装PhantomJS
PhantomJS官网下载,地址:http://phantomjs.org/download.html,根据自己系统环境选择安装。
#下载 [root@wrx install]# wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-i686.tar.bz2 --2017-07-21 17:43:35-- https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-i686.tar.bz2 Resolving bitbucket.org... 104.192.143.1, 104.192.143.2, 104.192.143.3 #解压 [root@wrx install]# tar -jxvf phantomjs-2.1.1-linux-i686.tar.bz2 phantomjs-2.1.1-linux-i686/ phantomjs-2.1.1-linux-i686/examples/ phantomjs-2.1.1-linux-i686/examples/colorwheel.js …… …… [root@wrx install]# cd phantomjs-2.1.1-linux-i686 [root@wrx phantomjs-2.1.1-linux-i686]# ls bin ChangeLog examples LICENSE.BSD README.md third-party.txt [root@wrx phantomjs-2.1.1-linux-i686]# pwd /var/data/install/phantomjs-2.1.1-linux-i686 #建立软链接 [root@wrx install]# sudo ln -s /var/data/install/phantomjs-2.1.1-linux-i686/bin/phantomjs /usr/local/bin/phantomjs #安装渲染所需(FreeType,Fontconfig)依赖 [root@wrx phantomjs-2.1.1-linux-i686]# yum install fontconfig freetype2 -y #查看是否安装成功 [root@wrx phantomjs-2.1.1-linux-i686]# phantomjs -v 2.1.1
3、Hello World
[root@wrx tmp]# vim helloworld.js #helloword.js内容 console.log("Hello World!")//此行会在运行时输出Hello World! phantom.exit();//phantom.exit退出,运行完后要注意退出 [root@wrx tmp]# phantomjs helloworld.js Hello World! [root@wrx tmp]#
4、访问一个页面并且保存页面为图片
#page.render [root@wrx tmp]# vim page.js var page = require('webpage').create(); page.open('http://www.sdlj.ltd/', function () { page.render('www.sdlj.ltd.png'); phantom.exit(); }); #执行 [root@wrx tmp]# phantomjs page.js #查看执行结果,可以看到当前目录下有一张名为www.sdlj.ltd.png图片 #如果查看图片时出现乱码或者无文字的情况,则需要安装字体,再次执行时就不会出现乱码 [root@wrx tmp]# yum install bitmap-fonts bitmap-fonts-cjk -y Loaded plugins: fastestmirror Setting up Install Process Determining fastest mirrors …… ……