Skip to content

Instantly share code, notes, and snippets.

@nysalor
Last active December 25, 2018 22:40
Show Gist options
  • Save nysalor/93f30a529588839ff9e521d2b943fd7d to your computer and use it in GitHub Desktop.
Save nysalor/93f30a529588839ff9e521d2b943fd7d to your computer and use it in GitHub Desktop.
艦これwikiから台詞リストを取得する
require 'uri'
require 'mechanize'
start_url = ARGV[0] || 'https://wikiwiki.jp/kancolle/%E8%89%A6%E5%A8%98%E3%82%AB%E3%83%BC%E3%83%89%E4%B8%80%E8%A6%A7'
agent = Mechanize.new
index_page = agent.get(start_url)
uri = URI.parse(start_url)
vessels = []
index_page.search('//*[@id="body"]/div[2]/table/tbody/tr[*]/td[*]/a').each do |link|
path = link.get_attribute(:href)
if path
uri.path = path
name = link.get_attribute(:title)
card_no = link.search('img').first.get_attribute(:title).split(':').first.to_i
vessels << { name: name, card_no: card_no, url: uri.to_s }
puts "#{name} (##{card_no}) -> #{uri}"
end
end
voices = []
vessels.each do |vessel|
vessel_page = agent.get(vessel[:url])
rows = vessel_page.search('//*[@id="body"]/table[1]').children.first.children[4].search('tr')
rows.shift
last_type = ''
rows.each do |row|
cols = row.children
if cols.size == 2
last_type = cols.shift.inner_text
end
puts "#{vessel[:name]} / #{last_type}"
voices << {
vessel: vessel[:name],
card_no: vessel[:card_no],
type: last_type,
text: cols.shift.inner_text
}
end
end
csvf = File.open('kc_voices.csv', 'w')
voices.each do |voice|
csvf.puts [voice[:card_no], voice[:vessel], voice[:type], voice[:text]].join(',')
end
csvf.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment