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

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

作者: 难道_fa74 | 来源:发表于2017-08-22 23:48 被阅读0次
    • 在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

    相关文章

      网友评论

          本文标题:ruby《王者荣耀》游戏官网皮肤图片爬取

          本文链接:https://www.haomeiwen.com/subject/bqmrdxtx.html