js と Cookie を有効にしつつクローリングしたい
- phantomjs コマンドに
--cookies-file
オプションでいわゆる cookie_jar のようにクッキーを保存するファイルを指定できる- ただこれをプログラムファイル側から扱う方法は無いみたい
phantom.open
はいわゆる onload のタイミングでコールバックを呼ぶが, それだと後から呼ぶ処理やサードパーティのスクリプトが動く前だったりするので, とりあえず setTimeout で逃げた- phantom.exit でプロセスは終わらない? プロセスを抜けるにはどうしたらいいんだろう
- リクエスト・レスポンスヘッダとかステータスコードをリクエストごとに出したいけどとれないっぽい
- phantomjs をバックエンドにした curl って作れないかなあ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint indent: 4*/ | |
/*globals document, phantom, require, console setTimeout*/ | |
/*jslint node: true */ | |
'use strict'; | |
var page = require('webpage').create(), | |
system = require('system'), | |
fs = require('fs'), | |
eol = system.os.name === 'windows' ? "\r\n" : "\n", | |
interval_sec = 3000, | |
content, f; | |
// Win IE8 | |
page.settings.userAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)'; | |
if (system.args.length < 2) { | |
console.log('Usage: phantomjs --cookies-file=/path/to/cookiefile ' + system.args[0] + ' URL_LIST_FILE'); | |
phantom.exit(1); | |
} | |
try { | |
f = fs.open(system.args[1], "r"); | |
} catch (e) { | |
console.error('Failed to open ' + system.args[1]); | |
console.error(e); | |
phantom.exit(1); | |
} | |
content = f.read(); | |
f.close(); | |
var url_list = content.split(eol); | |
loadPage(0, url_list); | |
function loadPage(index, url_list) { | |
var t = Date.now(), address; | |
if (index >= url_list.length) { | |
phantom.exit(); | |
} | |
address = url_list[index]; | |
if (!address) { | |
setTimeout(function () { | |
loadPage(index + 1, url_list); | |
}, interval_sec); | |
} else { | |
page.open(address, function (status) { | |
console.log(address + ' ... ' + status); | |
if (status !== 'success') { | |
console.error('FAIL to load the address'); | |
} else { | |
t = Date.now() - t; | |
console.log('Loading time ' + t + ' msec'); | |
} | |
setTimeout(function () { | |
console.log(JSON.stringify(phantom.cookies)); | |
loadPage(index + 1, url_list); | |
}, interval_sec); | |
}); | |
} | |
} |