- 在python中文社區(qū)看見(jiàn)了爬王者榮耀官網(wǎng)皮膚圖片的,于是心血來(lái)潮裙盾,想用ruby實(shí)現(xiàn)下
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數(shù)組
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