一、安裝PhantomJS(linux環境安裝)
將PhantomJS下載在/usr/local/src/packet/目錄下(這個看個人喜好)
操作系統:CentOS 7 64-bit
1.下載地址:http://phantomjs.org/download.html
2.文件名:phantomjs-2.1.1-linux-x86_64.tar.bz2
# 下載好後進行解壓(由於是bz2格式,要先進行bzip2解壓成tar格式,再使用tar解壓) bzip2 -d phantomjs-2.1.1-linux-x86_64.tar.bz2 # 再使用tar進行解壓到/usr/local/目錄下邊 tar xvf phantomjs-2.1.1-linux-x86_64.tar -C /usr/local/ # 安裝依賴軟件 yum -y install wget fontconfig # 重命名(方便以後使用phantomjs命令) mv /usr/local/phantomjs-2.1.1-linux-x86_64/ /usr/local/phantomjs # 最後一步就是建立軟連接了(在/usr/bin/目錄下生產一個phantomjs的軟連接,/usr/bin/是啥目錄應該清楚,不清楚使用 echo $PATH查看) ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/
到這一步就安裝成功了,接下來測試一下(經過上面建立的軟連接,你就可以使用了,而且是想使用命令一樣的進行使用哦!):
[root@localhost ~]# phantomjs
phantomjs>
接下來咱們建立一個文件來測試一下:
# 建立一個新文件 並寫入 console.log('Hello world!) phantom.exit(); [root@localhost roottest]# vim test.js # 查看一下 [root@localhost roottest]# cat test.js console.log('Hello world!'); phantom.exit();//這一行表示退出命令行 # 執行一下試試(OK了) [root@localhost roottest]# phantomjs test.js Hello world!
至於很多人可能很疑惑,進入命令行後怎麼退出命令行,目前本人知道的就是按 Ctrl + c 組合鍵退出和 phantom.exit();退出命令行了
[root@localhost roottest]# phantomjs phantomjs> exit(); Can't find variable: exit phantomjs://repl-input:1 in global code phantomjs> phantom.exit(); [root@localhost roottest]#
二、安裝PhantomJS(windows環境安裝)
1.下載的文件名:phantomjs-2.1.1-windows.zip,直接解壓出來,解壓後的文件複製到你電腦的任意盤(我是放在D盤),建議別放c盤,然後進入解壓目錄,phantomjs-2.1.1\bin下有個phantomjs.exe,雙擊就可以進入命令行了;
2.添加環境變量,在cmd中就可使用phantomjs命令了,添加完之後就是以下界面:
OK,部署完成
三、使用PhantomJS
網址1:http://javascript.ruanyifeng.com/tool/phantomjs.html
網址2(官網):http://phantomjs.org/api/webpage/
我就不多敘述了,自己看
感謝原博客地址:https://blog.csdn.net/johinieli/article/details/76622776