ラベル CasperJS の投稿を表示しています。 すべての投稿を表示
ラベル CasperJS の投稿を表示しています。 すべての投稿を表示

2015年11月20日金曜日

CasperJSでログインしてみよう

このエントリーをはてなブックマークに追加

今回はCasperJSを使って、よく使う朝日新聞デジタルの無料会員IDでログインをしてみる。

ログイン部分のソースを確認して,form名やテキストボックスの名前を取得する。
var user = "ユーザ名";
var pass = "パスワード";

//CasperJSオブジェクトの生成
var casper = require('casper').create();

// これ以降、run()メソッドまでの前の処理がrun()実行時に行われる
casper.start();

// ログインページへ接続
casper.open('https://digital.asahi.com/login/');

// ログインする
casper.then(function(){
// フォームを特定し、ユーザ・パスワードを入力する。
 this.fill('form[action="login.html"]', {
  login_id: user,
  login_password: pass
 }, true);
});

// ログイン後のキャプチャをとる
casper.then(function(){
 casper.capture("asahi.png");
});

casper.run();

ログインできた。


結構、なんでもサイトをいじれて便利。いろんなツールとの組み合わせでテストなどにも使えそうだ。

今日の作業

3連休、留守にします。

2015年11月18日水曜日

CasperJSでFlickrの検索結果を取得する

このエントリーをはてなブックマークに追加
先ほどの画面キャプチャの応用というか、横展開というか。

// CasperJSオブジェクトの生成
var casper = require('casper').create();

// これ以降、run()メソッドまでの前の処理がrun()実行時に行われる
casper.start();

// 画面サイズを設定
casper.viewport(1400, 800);

// UserAgent設定
casper.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");

// 検索ワード
var text = encodeURIComponent("大阪");

// サイトへ接続
casper.open('https://www.flickr.com/search/?text=' + text);

// サイト接続後、capture取得
casper.then(function(){
 casper.capture("flickr-image.png",{top:0,left:0,width:1400,height:800});
});

// 処理開始
casper.run();

できたできた。


ユーザエージェントと画面サイズの変更してあげれば、iPhoneとかスマートフォンのふりをすることも可能。


今日の作業

触りで終わってしまった。

(続)CasperJSで画面キャプチャを撮る〜日本語を利用できるようにする

このエントリーをはてなブックマークに追加
タイトルの通り、昨日の続き。なるほど、TTFフォントをインストールしていないからフォントが豆腐になったのか。

LinuxなのでIPAフォントを使ってみよう。書体はゴシックでいいや。

# wget http://dl.ipafont.ipa.go.jp/IPAexfont/ipaexg00301.zip
# unzip ipaexg00301.zip
# mkdir /usr/share/fonts/ipaexg00301
# cp -ip ipaexg00301/ipaexg.ttf /usr/share/fonts/ipaexg00301/
# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/ipaexg00301: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/X11/fonts/Type1: skipping, no such directory
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/X11/fonts/Type1: skipping, no such directory
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded
確認。
# fc-list
/usr/share/fonts/ipaexg00301/ipaexg.ttf: IPAexゴシック,IPAexGothic:style=Regular

キャプチャスクリプトを再実行
# casperjs screenshot.js


できたー

2015年11月17日火曜日

CasperJSで画面キャプチャを撮る

このエントリーをはてなブックマークに追加
CasperJSで画面キャプチャも取れるらしい。マニュアルとか作る時超便利やんなんか思いながらやってみる。

// CasperJSオブジェクトの生成
var casper = require('casper').create();

// これ以降、run()メソッドまでの前の処理がrun()実行時に行われる
casper.start();

// サイトへ接続
casper.open('http://www.google.co.jp');
// サイト接続後、capture取得
casper.then(function(){
 casper.capture("screenShot.png");
});

// 処理開始
casper.run();

キャプチャを開くと文字がない。

なんでなんだろう。。。

今日の作業

文字が出ないのが残った。。。

2015年11月16日月曜日

PhantomJSとCasperJS

このエントリーをはてなブックマークに追加

世の中にはいろいろ便利なJavaScriptのライブラリがあるものです。今回はコマンドラインから利用できるPhantomJSと、そのPhantomJSを便利に使うことができるようになるCasperJSを使ってみる。

まずはインストールから。npmを使う。
# /usr/local/node/bin/npm install -g phantomjs
/usr/local/src/node-v4.2.2-linux-x64/bin/phantomjs -> /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/bin/phantomjs

> phantomjs@1.9.18 install /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs
> node install.js

Looks like an `npm install -g`; unable to check for already installed version.
Downloading https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2
Saving to /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/phantomjs/phantomjs-1.9.8-linux-x86_64.tar.bz2
Receiving...
  [=================================-------] 82% 0.0s
Received 12854K total.
Extracting tar contents (via spawned process)
Removing /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/lib/phantom
Copying extracted folder /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/phantomjs/phantomjs-1.9.8-linux-x86_64.tar.bz2-extract-1447702852021/phantomjs-1.9.8-linux-x86_64 -> /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/lib/phantom
Writing location.js file
Done. Phantomjs binary available at /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs
phantomjs@1.9.18 /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs
├── which@1.0.9
├── progress@1.1.8
├── kew@0.4.0
├── adm-zip@0.4.4
├── request-progress@0.3.1 (throttleit@0.0.2)
├── request@2.42.0 (tunnel-agent@0.4.1, forever-agent@0.5.2, aws-sign2@0.5.0, caseless@0.6.0, oauth-sign@0.4.0, stringstream@0.0.5, json-stringify-safe@5.0.1, tough-cookie@2.2.1, mime-types@1.0.2, qs@1.2.2, node-uuid@1.4.7, http-signature@0.10.1, hawk@1.1.1, form-data@0.1.4, bl@0.9.4)
├── npmconf@2.1.1 (ini@1.3.4, inherits@2.0.1, uid-number@0.0.5, semver@4.3.6, once@1.3.2, mkdirp@0.5.1, nopt@3.0.6, config-chain@1.1.9, osenv@0.1.3)
└── fs-extra@0.23.1 (path-is-absolute@1.0.0, jsonfile@2.2.3, graceful-fs@4.1.2, rimraf@2.4.3)
# /usr/local/node/bin/npm install -g casperjs
|
> phantomjs@1.9.18 install /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs
> node install.js

Looks like an `npm install -g`; unable to check for already installed version.
Downloading https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.8-linux-x86_64.tar.bz2
Saving to /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs/phantomjs/phantomjs-1.9.8-linux-x86_64.tar.bz2
Receiving...
  [===================================-----] 88% 0.0s
Received 12854K total.
Extracting tar contents (via spawned process)
Removing /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom
Copying extracted folder /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs/phantomjs/phantomjs-1.9.8-linux-x86_64.tar.bz2-extract-1447702982784/phantomjs-1.9.8-linux-x86_64 -> /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom
Writing location.js file
Done. Phantomjs binary available at /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/node_modules/phantomjs/lib/phantom/bin/phantomjs
/usr/local/src/node-v4.2.2-linux-x64/bin/casperjs -> /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs/bin/casperjs
casperjs@1.1.0-beta3 /usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/casperjs
└── phantomjs@1.9.18 (which@1.0.9, progress@1.1.8, kew@0.4.0, adm-zip@0.4.4, request-progress@0.3.1, npmconf@2.1.1, request@2.42.0, fs-extra@0.23.1)

サイトのタイトルを取ってくる簡単なサンプルを書いて実行してみる。
// 取得するURLを設定
var TARGET_URL = "http://smile-smile-at-smile.blogspot.jp/";

// CasperJSオブジェクトの生成
var casper = require('casper').create();

// 指定のサイトを開く
casper.start(TARGET_URL, function(){
 // サイトタイトルの表示
 this.echo(casper.getTitle());
});

// このrun()メソッドが呼ばれた時点で
// start()メソッドに登録した処理が実行される
casper.run();

実行したら怒られた。
# casperjs getTitle.js
/usr/local/src/node-v4.2.2-linux-x64/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs: error while loading shared libraries: libfontconfig.so.1: cannot open shared object file: No such file or directory

fontconfigをインストール。
# yum install fontconfig

再度、実行してみる。
# casperjs getTitle.js
いつも君は僕のPAYDAYを取り上げるんだ

できたじゃん。

今日の作業

OpenCVの画像検出できた時はすごいうれしかった!あとは黙々とパース処理。。。