LoginSignup
0
0

More than 5 years have passed since last update.

Spooky.jsでファイルのダウンロード - スクレイピング(環境構築編)

Last updated at Posted at 2017-09-05

とあるWebシステムから、ファイルを自動でダウンロードしたかったので、
Phantom.js+Spookyjsでやってみる。

参考
http://wordpress.ideacompo.com/?p=8649

環境

  • Ubuntu 16.04.2 LTS
  • Node.js 8.0.0
  • Phantom.js 2.1.1
  • Casper.js 1.1.4
  • Spooky.js 1.8
  • tiny-jsonrpc 2.0.1

環境構築手順

Node.jsのインストール

$ sudo apt-get install nodejs npm

最新版を入れたい場合は、Node.jsをバージョン管理できるnvmやgをご利用ください。

Phantom.jsとCasper.jsのインストール

「apt-getからインストール」、「npmからインストール」、「ソースコードからインストール」の3パターンがありますが、前者はよくトラブるので、ソースコードからインストールをお勧めします。

$ cd /usr/local/share

# Phantom.js
$ wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
$ sudo tar jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
$ sudo mv phantomjs-2.1.1-linux-x86_64 phantomjs
$ sudo ln -sfn /usr/local/share/phantomjs/bin/phantomjs /usr/local/bin/

# Casper.js
$ git clone https://github.com/casperjs/casperjs.git
$ ln -s /usr/local/share/casperjs/bin/casperjs /usr/local/bin/

# 確認
$ phantomjs --version
2.1.1
$ casperjs --version
1.1.4

Spooky.jsのインストール

Spooky.jsのインストールはnpmを使用します。

$ npm install spooky

今回はローカル環境にインストールしてます。

Tiny JSON-RPCのインストール

これがないと起動できないので、インストール

$ npm install tiny-jsonrpc

Spooky.jsのコメント不具合を直す

Spookyには、日本語コメントが含まれていると動作が止まってしまう不具合が存在する(2017/9/5現在)
それを修正する。

対象ファイル
node_modules/spooky/lib/spooky/request-stream.js

 RequestStream.prototype.write = function write(packet, callback) {
     var options = _.clone(this.options);
-    options.headers['Content-Length'] = packet.length;
+    options.headers['Content-Length'] = Buffer.byteLength(packet, 'utf8');

     var req = http.request(options, this._onResponse.bind(this, callback));
     req.on('error', this._onError.bind(this, callback));

動作確認

公式ページに書かれているサンプル例をそのままファイルに保存して、動かしてみます。
https://github.com/SpookyJS/SpookyJS

$ node example.js
Hello, from Spooky the Tuff Little Ghost - Wikipedia
0
0
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
0
0