sudo apt-get install phantomjs
wget https://bitbucket.org/ariya/p...c++
wget https://bitbucket.org/ariya/p...web
unzip phantomjs-2.0.0-source.zipsql
yum -y install gcc gcc-c++ make flex bison gperf ruby \
openssl-devel freetype-devel fontconfig-devel libicu-devel sqlite-devel \
libpng-devel libjpeg-develubuntu
cd phantomjs-2.0.0
./build.sh
编译操做耗时会比较长(能够大于半个小时),耐心等待完成。编译完成后会多出一个 bin 目录,里面有编译完成的 phantomjs 文件。ruby
添加到系统常量:函数
cp phantomjs /usr/local/bin
查看是否安装成功:flex
phantomjs --version
使用案例(先添加了js文件好比:profile.js,代码以下):ui
var page = require('webpage').create(), system = require('system'), url; if(system.args.length === 1){ phantomjs.exit(1); }else{ url = system.args[1]; //开始渲染url page.open(url,function (status){ if(status !== 'success'){ phantom.exit(); }else{ var sc =page.evaluate(function (){ return document.body.innerHTML; }); window.setTimeout(function (){ console.log(sc); phantom.exit(); },1000) } }); } //睡眠函数 function sleep(ms){ console.log('start s:'+new Date()/1000); ms += new Date().getTime(); while(new Date()<ms){ //console.log('ms:'+new Date()/1000); } console.log('end s:'+new Date()/1000); }
使用命令:lua
# 使用phantomjs将渲染网页,而且将结果写入到/tmp/test8.txt中,能够供后续程序抓取所需数据 # phantomjs js文件 目标网页 phantomjs profile.js https://nike.tmall.com/view_shop.htm 1> /tmp/test8.txt