- 在python中文社区看见了爬王者荣耀官网皮肤图片的,于是心血来潮,想用ruby实现下
require 'unirest'
require 'open-uri'
url = 'http://pvp.qq.com/web201605/js/herolist.json'
response = Unirest.get(url)
hero_list = response.body
#返回ename, cname, skin_name组成的hash数组
hero_list.each do |hero|
hero.select!{|key, _| key=='ename' || key == 'cname' || key == 'skin_name'}
end
def download_hero_img(url, path, img_name)
img_file = open(url,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE){ |f| f.read }
puts "download #{path}/#{img_name}.jpg"
open("#{path}/#{img_name}.jpg", 'wb'){ |f| f.write(img_file)}
end
hero_list.each do |hero|
path = "./#{hero['cname']}"
Dir.mkdir(path) unless Dir.exists?(path)
skin_list = hero['skin_name'].split('|')
skin_list.each_with_index do |skin, index|
url = "https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/#{hero['ename']}/#{hero['ename']}-bigskin-#{index.to_i+1}.jpg"
img_name = skin
download_hero_img(url, path, img_name)
end
end
python方式参考地址:https://zhuanlan.zhihu.com/p/28617629