参考:

https://www.cnblogs.com/10-22/articles/4383196.html

https://www.cnblogs.com/daysme/p/10250224.html

https://www.jianshu.com/p/fd76e68ce59b

1.下载

官网:https://phantomjs.org/download.html

2.安装依赖

yum install -y bzip2 # 安装bzip2

yum install -y fontconfig freetype2

yum install bitmap-fonts bitmap-fonts-cjk

yum groupinstall "fonts" -y # 安装字体相关的依赖包

fc-cache # 刷新字体缓存

3.安装

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

4.调试

查看版本

phantomjs -v # 测试版本号

测试脚本test.js (返回指定网页内第一个div的内容)

var page = require('webpage').create(),

    system = require('system'),

    address;

address = system.args\[1\];

page.open(address, function (status) {

    if (status !== 'success') {

        console.log('Unable to access network');

    } else {

        var ua = page.evaluate(function () {

            return document.getElementsByTagName('div')\[0\].textContent;

        });

        console.log(ua);

    }

    phantom.exit();

});

执行脚本

phantomjs test.js http://xxx.com/test.html

5.在PHP中调用

exec("phantomjs /script/test.js http://xxx.com/test.html");

IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生