1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PhantomJSについて

Posted at

PhantomJSとは

webkitベースのヘッドレスブラウザ。webkitとは主にWebブラウザーで用いられているレンダリングエンジン。PhantomJSはwebkitにbuilt-inされているJavaScriptCore (safariとかもこれを使っている) というjavascriptエンジンを採用している。スクレイピングやスクリーンキャプチャなどにも活用可能。

ヘッドレスブラウザ:ヘッドレス・ブラウザとは、Webコンテンツを画面上に描くグラフィック・ユーザ・インタフェース(GUI)を省略したブラウザ

要はコマンド(プログラム)でブラウザを実行できる環境

プログラムで動きを制御できるので、
「自動化テスト」
「スクレイピング」
などを行なってくれます。

Node.jsを使ったインストール方法

$ brew install nodebrew
$ nodebrew setup//これをしないと次のコマンドでエラー吐く。必要なフォルダ等々を生成してくれるらしい
$ echo export PATH=$HOME/.nodebrew/current/bin:$PATH >> ~/.bash_profile //書き込み
$ source ~/.bash_profile //読み込み
$ nodebrew install-binary latest //最新版のインストール
$ nodebrew list //インストール確認
$ npm -v //バージョン確認
$ nodebrew use v11.1.0 //使用するバージョンを指定することも可能
$ npm install -g phantomjs

Node.jsでなくても

$ brew cask install phantomjs

こちらの方法でも可能

Railsのスクレイピングで私が使っているCapybara/Poltergeist

Capybara = UIテストのためのrubyフレームワーク

オープンソース / MITライセンス
ページの表示、フォームの入力、ボタンのクリック等のUI操作をテストコード上で実行できる

複数のテストフレームワークから利用できる

MiniTest, RSpec, Cucumberなど
複数の ドライバ (テスト環境, ヘッドレスブラウザ)を選択して利用できる

Rack::Test, Selenium, Webkitなど

poltergeist

Capybaraのドライバーの一つです。
他にもselenium, rack-test(デフォルトのドライバー)などがあります。rack-testは高速ですが、JSに対応してなかったり、seleniumはJSに対応してるがヘッドレスではないため処理が遅かったりと、それぞれ一長一短です。

Gemのインストール

$ gem 'capybara'
$ gem 'poltergeist'

bundle installをしましょう。

PhantomJSの今後

PhantomJSの開発は終了し、リポジトリはアーカイブ化されました。本記事は参考程度にご覧ください。
参考

ですので、現在私は他の方法も模索中です。
Puppeteer + Headless Chromeの方法なども気になります。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?