雨冷青杉 发表于 2019-10-7 17:02:02

【Python】爬取王者荣耀全英雄全皮肤高清大图

'''
爬取王者荣耀全英雄全皮肤高清图
'''

import urllib.request
import os
import time

#打开一个页面
def open_page(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36')
    reponse = urllib.request.urlopen(req)
    html = reponse.read()
    return html

#获取英雄:编号为键值对的字典,并为每个英雄创建一个文件夹
def get_hero(str):
    hero_dict = {}
    num_a = str.find('src="//game.gtimg.cn/images/yxzj/img201606/heroimg/') + 51

    while num_a != 50:
      num_b = num_a + 3
      hero_a = str.find('alt=',num_b) + 5
      hero_b = str.find('">', hero_a)
      hero = str
      num = str
      hero_dict = num
      os.mkdir(hero)
      num_a = str.find('src="//game.gtimg.cn/images/yxzj/img201606/heroimg/', num_b) + 51

    return hero_dict

#获取当前英雄皮肤名称:皮肤编号为键值对的字典
def get_skin(str):
    skin_dict = {}

    skin_a = str.find('data-imgname="') + 14
    skin_b = str.find('">', skin_a)
    skin = str
    skin_list = skin.split('|')
    for i in range(len(skin_list)):
      skin_dict] = i + 1

    return skin_dict

#通过传入的皮肤名称和编号进行图片保存
def save_skin(hero_name, name, skin_num, hero_num):
    url = f'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{hero_num}/{hero_num}-bigskin-{skin_num}.jpg'
    html = open_page(url)
    file_name = hero_name + ' - ' + name + '.jpg'
    with open(file_name, 'wb') as f:
      f.write(html)
      time.sleep(3)

#定义主函数
def honor_skin():
    cnt = 0
    url_main = 'https://pvp.qq.com/web201605/herolist.shtml'
    url_part = 'https://pvp.qq.com/web201605/herodetail/'
    dirs = '荣耀皮肤'
    os.mkdir(dirs)
    os.chdir(dirs)

    html_main = open_page(url_main).decode('gbk')
    hero_dict = get_hero(html_main)

    for i in hero_dict:
      hero_num = hero_dict
      url_hero = url_part + hero_num + '.shtml'
      html_hero = open_page(url_hero).decode('gbk')
      skin_dict = get_skin(html_hero)
      os.chdir(i)
      for key, value in skin_dict.items():
            save_skin(i, key, value, hero_num)
            cnt += 1
            print(f'已成功保存{cnt}张皮肤,别鸡巴着急,等着的....')
      print(f'{i} 的皮肤已下载完毕!')
      os.chdir('..')

#好习惯要坚持
if __name__ == '__main__':
    honor_skin()

雨冷青杉 发表于 2019-10-8 14:46:35

斯国一

六小鸭 发表于 2020-3-18 11:31:02

这么好的帖子居然沉了{:10_324:}
页: [1]
查看完整版本: 【Python】爬取王者荣耀全英雄全皮肤高清大图