王者荣耀英雄皮肤(20200626更新)

文章目录

          • 1. 上篇地址
          • 2. 更新源码

1. 上篇地址

https://blog.csdn.net/gklcsdn/article/details/102647286

2. 更新源码

import re
import os
import requests


class Spider(object):
    def __init__(self):
        self.hero = 'https://pvp.qq.com/web201605/js/herolist.json'

    def get_hero_name(self):
        """
        获取编号和英雄名
        :return:
        """
        response = requests.get(self.hero).json()
        e_name_list = []
        c_name_list = []
        for data in response:
            # 编号
            ename = data['ename']
            # 英雄名
            cname = data['cname']
            e_name_list.append(ename)
            c_name_list.append(cname)

        return e_name_list, c_name_list


    def get_skin_name(self, e_name_list, c_name_list):
        """

        :param e_name_list: 英雄编号列表
        :param c_name_list: 英雄名列表
        :return:
        """

        for num, folder_name in zip(e_name_list, c_name_list):
            # 文件夹创建
            folder = './img/{}'.format(folder_name)
            if not os.path.exists(folder):
                os.mkdir(folder)

            # 网页源码获取皮肤名
            url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'.format(num)

            response = requests.get(url).content.decode('gbk')
            skin_name = re.findall(r'data-imgname="(.*?)"', response)[0].split('|')

            # 单个英雄皮肤个数
            length = len(skin_name)
            skin_name_list = []
            for name in skin_name:
                skin_name_list.append(name)


            skin_url_list = []
            for i in range(1, length+1):
                skin_url = 'https://game.gtimg.cn/images/yxzj/img201606/heroimg/{0}/{0}-mobileskin-{1}.jpg'.format(num, i)

                skin_url_list.append(skin_url)


            for skin_download_url, skin_download_name in zip(skin_url_list, skin_name_list):
                response = requests.get(skin_download_url).content

                name = skin_download_name.split('&')[0]
                save_name = folder + "/" + name + '.jpg'
                print(save_name)
                with open(save_name, 'wb') as f:
                    f.write(response)


if __name__ == '__main__':
    s = Spider()
    e_name_list, c_name_list = s.get_hero_name()
    s.get_skin_name(e_name_list, c_name_list)

王者荣耀英雄皮肤(20200626更新)_第1张图片

你可能感兴趣的:(Crawler)