サーバ側から画面キャプチャを取ろうと
いろいろやってみた
そのうちのひとつ「casperjs」を試してみたけれど
結構簡単だったのでメモメモ
// まずは「phantomjs」っていうのをインストール $ wget https://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2 $ tar jxfv phantomjs-1.9.1-linux-x86_64.tar.bz2 # cp phantomjs-1.9.1-linux-x86_64/bin/phantomjs /usr/local/bin/ // いよいよ「casperjs」をインストール $ wget https://github.com/n1k0/casperjs/archive/1.1-beta1.tar.gz $ tar zxfv 1.1-beta1.tar.gz $ cd casperjs-1.1-beta1/ # ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs
それぞれ新しいバージョン出てたのでこちら↓のほうがよいかも~
// まずは「phantomjs」っていうのをインストール $ wget --secure-protocol=TLSv1 https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 $ tar jxfv phantomjs-2.1.1-linux-x86_64.tar.bz2 $ cd phantomjs-2.1.1-linux-x86_64.tar.bz2 # ln -sf `pwd`/bin/phantomjs /usr/local/bin/phantomjs // いよいよ「casperjs」をインストール $ wget https://github.com/n1k0/casperjs/archive/1.1-beta5.tar.gz -O casperjs.1.1-beta5.tar.gz $ tar zxfv casperjs.1.1-beta5.tar.gz $ cd casperjs-1.1-beta5/ # ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs
試しに、「test.js」ってのを作ってみる
var casper = require('casper').create(); casper.start('http://google.com/', function() { this.capture('google.png'); }); casper.run();
作ったファイルを実行!
casperjs test.js
もし日本語が豆腐さんになってたら、
日本語フォントを入れてあげる
以前の手順と一緒
それでもダメなら
export LANG=en_US.UTF-8
きっとこれで解決するはず
cronにしかけたshellからphpを実行するときに
casperjsコマンドをたたくと、下記エラーが。。。
手でshellを実行するぶんには問題ないのに
Fatal: [Errno 2] No such file or directory; did you install phantomjs?
phantomjsの場所がわかっていないようなので
shellに下記の1行を追加
export PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs
phantomjsの場所は、下記コマンドで調べましょー
whereis phantomjs
コメント