Created
October 4, 2015 23:36
-
-
Save thr3a/69a8ace9b91426215414 to your computer and use it in GitHub Desktop.
例のWIKIからスクレイピングするやつ ex. app.rb > denko.json
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
require 'mechanize' | |
require 'json' | |
result = [] | |
top_url = 'http://stationmemories.wiki.fc2.com/' | |
wiki = Mechanize.new.get(top_url) | |
wiki.search('ul.treemenu_ul')[0].css('li a').each do|denko| | |
# でんこ個別ページ取得 | |
page = Mechanize.new.get(top_url + denko[:href]) | |
# でんこの名前取得 | |
name = page.search('.page_title h1').text | |
array = [] | |
page.search("table").each do |table| | |
# ステータス表を検索 | |
next if table.css("th").text != "LvExpAPHP" | |
table.css("tr:gt(1)").each do |row| # 最初の<tr>は列名なのでスルー | |
array << row.css('td').map{ |n| n.text.to_i } #ex ["1", "0", "50", "72"] | |
end | |
end | |
# 行と列を入れ替える | |
lv, ex, ap, hp = array.transpose | |
result << {name: name, AP: ap, HP: hp} | |
end | |
puts JSON.pretty_generate(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment