Skip to content

Instantly share code, notes, and snippets.

@tooooolong
Last active December 18, 2015 06:59
Show Gist options
  • Save tooooolong/5743139 to your computer and use it in GitHub Desktop.
Save tooooolong/5743139 to your computer and use it in GitHub Desktop.
多玩 英雄联盟数据抓去脚本
# require "rubygems"
require 'open-uri'
require "nokogiri"
doc = Nokogiri::HTML(open("http://lol.duowan.com/s/heroes.html"))
heros = []
error = []
doc.css("ul#champion_list li").each do |node|
hero = {}
hero[:name] = node.css("div.champion_name").inner_text
hero[:title] = node.css("div.tooltip h3").inner_text
hero[:description] = node.css("div.tooltip p").inner_text
hero[:tags] = node.css(".champion_tooltip_tags").inner_text.gsub("Tags: ", "")
hero[:img] = node.css("img.champion_icon").attribute("src").to_s
link = node.css("a").attribute("href")
begin
detail = Nokogiri::HTML(open(link))
rescue Exception => e
error << link
next
end
detail.css("div.zy").each do |dnode|
dnode.css("div.txt-fl p").each_with_index do |p, pindex|
case pindex
when 0
p.css("span").each_with_index do |gold, index|
case index
when 0
hero[:voucher] = gold.inner_text.to_i
when 1
hero[:gold] = gold.inner_text.to_i
end
end
when 1
p.css("span").each_with_index do |gold, index|
case index
when 0
hero[:a_voucher] = gold.inner_text.to_i
when 1
hero[:a_gold] = gold.inner_text.to_i
end
end
end
end
hero[:score] = {}
dnode.css("div.hero-fl-txt .value > div").each do |score|
num = score.css("span").attribute("style").to_s.gsub(/\D/, "").to_i
num = num / 10
hero[:score][score.css("em").inner_text] = num
end
hero[:detail] = {}
dnode.css("div.hero-sz li").each do |li|
at_desc = li.css("p").inner_text.gsub(/:|:/, "")
hero[:detail][at_desc] = li.css("span").inner_text
end
hero[:spells] = []
dnode.css("div.mod-pic-txt-bd").each do |spill|
spill_data = {}
spill_data["img"] = spill.css("li img").attribute("src").to_s
spill_info = spill.css("li .fl h4").inner_text
array = spill_info.split("[ ")
spill_data["name"] = array[0]
spill_data["key"] = array[1].gsub(" ]", "")
spill_data["desc"] = spill.css("li .fl p").inner_text
spill.css("li .sz p").each_with_index do |info, iindex|
case iindex
when 0
spill_data["cooldown"] = info.inner_text.gsub("冷却时间:", "")
when 1
spill_data["cost"] = info.inner_text.gsub("施法消耗:", "")
when 2
spill_data["round"] = info.inner_text.gsub("施法距离:", "").gsub("施法半径:", "")
end
end
hero[:spells] << spill_data
end
end
heros << hero
end
puts heros
puts error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment