Skip to content
Create a gist now

Instantly share code, notes, and snippets.

戦コレDB のデータを CSV に落とす。
#!/usr/bin/env ruby
# coding: utf-8
require 'active_support/all'
require 'open-uri'
require 'nokogiri'
require 'cgi'
require 'ir_b'
def parse(no, body)
doc = Nokogiri::HTML(body)
text = doc.text
sub, name = doc.to_s.scan(/<\/span>(?:<br>)?(?:\[(.*)?\])?(.*?)<span/)[2]
return unless name
name = CGI.unescapeHTML(name)
type ='span')[1].text rescue nil
type_a = { '' => 'R', '' => 'B', '' => 'G' }[type]
num = text.scan(/No\.(\d+)/)[0][0].to_i
gender = text.scan(/♂|♀/)[0]
rare = text.scan(/S?(?:レア|ノーマル)\+?/)[0]
rare = rare.sub(/レア/,"R").sub(/ノーマル/,"N") unless rare.empty?
magic = text.scan(/魔力(\d+)/).join
atk, matk = text.scan(/攻撃力(\d+)(\d*)/)[0]
deff, mdeff = text.scan(/防御力(\d+)(\d*)/)[0]
skill ="div.skill span a").try(:text)
[no, type, num, gender, rare, sub, name, magic, atk, deff, matk, mdeff, skill, type_a]
def direct(no)
$stderr.puts "getting{no}"
body = open("{no}", 'r:cp932'){|f|"utf-8") }
parse(no, body)
def output_csv(from, to, out = $stdout)
to ||= from
( do |no, idx|
sleep(rand(5) + 5) unless idx == 0
result = direct(no)
out.puts result.join(',') if result
def main(args)
case args.length
when 0
from = 1
to = 890
when 1
from = args[0].to_i
to = nil
from = args[0].to_i
to = args[1].to_i
from = 1 if
to = from if to.nil? ||
output_csv(from, to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.