ruby《王者荣耀》游戏官网皮肤图片爬取

2017-08-22  本文已影响0人  难道_fa74

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

上一篇下一篇

猜你喜欢

热点阅读