Created
December 23, 2013 23:25
-
-
Save tekkoc/8106572 to your computer and use it in GitHub Desktop.
PhantomJSでqiitaにログインしてフィードのタイトルを取得、ファイルに書き込むサンプル。 [JavaScript - PhantomJSで簡単にログイン・スクレイピング・ページキャプチャ - Qiita [キータ]](http://qiita.com/tekkoc/items/f610289c7ce36f680d94)
This file contains hidden or 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
USER_ID = "qiitaのユーザ名" | |
PASSWORD = "qiitaのパスワード" | |
# initialize {{{ | |
page = require("webpage").create() | |
fs = require("fs") | |
page.onConsoleMessage = (msg) -> console.log("CONSOLE: " + msg) | |
# ページが読み込まれたら page.onCallback を呼ぶ | |
page.onInitialized = -> | |
page.evaluate -> | |
document.addEventListener "DOMContentLoaded", -> | |
window.callPhantom("DOMContentLoaded") | |
, false | |
class Actions | |
actions: [] | |
constructor: -> | |
page.onCallback = (data) => | |
@run() if (data == "DOMContentLoaded") | |
# アクションを追加 | |
add: (action) -> | |
action.name ?= "" | |
action.render ?= false | |
action.delay ?= 0 | |
action.before ?= (-> {}) | |
action.evaluate ?= ((obj) -> {}) | |
action.after ?= ((obj) -> {}) | |
@actions.push action | |
# アクションを順に実行 | |
run: -> | |
action = @actions.shift() | |
unless action? | |
phantom.exit() | |
return | |
# 指定ミリ秒数待ってから実行 | |
setTimeout => | |
console.log "==============================" | |
console.log " #{action.name}" | |
console.log "==============================" | |
if action.render | |
page.render("#{action.name}.png") | |
before_result = action.before() | |
# 各ページでライブラリを読み込む | |
page.injectJs "lib/jquery.min.js" | |
page.injectJs "lib/jquery.cookie.js" | |
# 開いているページ内でaction.evaluateを実行 | |
# action.beforeの結果を引数として受け取り、返り値をaction.afterの引数にする | |
evaluate_result = page.evaluate action.evaluate, before_result | |
action.after(evaluate_result) | |
if @actions.length == 0 | |
phantom.exit() | |
, action.delay | |
# }}} | |
### | |
以下、qiitaにログインしてフィードのタイトルを取得するサンプル | |
### | |
actions = new Actions | |
actions.add | |
name: "start" | |
after: -> | |
# URLを指定して開く | |
page.open("http://qiita.com/") | |
actions.add | |
name: "login" | |
render: true # 自動でlogin.pngとしてキャプチャ撮る | |
before: -> | |
# evaluateで参照する用のオブジェクトを定義し、return | |
user_id: USER_ID | |
password: PASSWORD | |
evaluate: (params) -> | |
# beforeの返り値をparamsとして受け取っている | |
# 「http://qiita.com/」内での処理(jQuery読み込み済み) | |
$("#user_url_name").val(params.user_id) | |
$("#user_password").val(params.password) | |
$("#new_user").submit() | |
actions.add | |
name: "mypage" | |
delay: 3000 # フィードの読み込みのため3秒待つ | |
evaluate: -> | |
# タイトルを表示(onConsoleMessageを定義してるから出力を確認出来る) | |
console.log $("title").text() | |
# 読み込んだフィードを順に処理 | |
titles = [] | |
$(".item-box-title").each -> | |
# 配列に追加 | |
titles.push $(@).find("h1 a").text().trim() | |
# afterで参照する用のオブジェクトを定義し、return | |
titles: titles | |
after: (result) -> | |
# evaluateの返り値をresultとして受け取っている | |
# ファイルに書き込み | |
fs.write "result.json", JSON.stringify(result.titles), true | |
actions.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment