Created
August 7, 2013 12:36
-
-
Save dogancelik/6173677 to your computer and use it in GitHub Desktop.
(PhantomJS) Parse Bleach episodes in Wikipedia
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
page = require('webpage').create() | |
fs = require 'fs' | |
system = require 'system' | |
page.onConsoleMessage = (msg) -> console.log(msg) | |
page.open "http://en.wikipedia.org/wiki/Bleach_episodes", (status) -> | |
csv = page.evaluate -> | |
formatDate = (date) -> | |
date.getFullYear() + "-" + ((if date.getMonth() < 9 then "0" else "")) + (date.getMonth() + 1) + "-" + ((if date.getDate() < 10 then "0" else "")) + date.getDate() | |
ret = "" | |
i = 1 | |
totalEps = 366 | |
while i <= totalEps | |
text = $("#ep" + i).parent().children("td.summary").text() | |
try | |
[_, englishTitle, romajiTitle, kanjiTitle] = text.match(/"(.*)"\s"(.*)"\s\((.*)\)/) | |
date = $("#ep#{i}").parent().children("td.summary").next().text().replace(/\[.*\]/gi,'') | |
date = formatDate(new Date(date)) | |
catch e | |
console.log "Episode: ", i | |
console.log "Error:", e | |
return | |
ret += "\"#{i}\", \"#{englishTitle}\", \"#{romajiTitle}\", \"#{kanjiTitle}\", \"#{date}\"\n" | |
i++ | |
ret | |
filename = system.args[1] or "bleach_episodes.csv" | |
fs.remove filename | |
fs.write filename, csv, "w" | |
phantom.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've also used XPath. This was in my notes:
//table[@class="wikitable" and position()<17]//td[position()<4]