Use Python to complete the download of the full skin of the hero of the glory of the king

Use Python to complete the download of the full skin of the hero of the glory of the king

This article uses python's third-party module requests to crawl the pictures of all heroes of the glory of the king, and save the pictures into a folder for each hero, which is convenient to use as a desktop wallpaper.

Basic environment configuration

Version: Python3

System: Windows

Related modules: requests

Install the module:

pip install requests

Complete code

# -*- coding: utf-8 -*-
Created on Wed Dec 13 13:49:52 2017

import requests
import os
hero_list_url =''
hero_skin_root_url =''
skin_base_dir ='C:\\Users\\lwt27\\Pictures\\image\\heroskin\\'

def get_ename(hero_json):#pass in the obtained python object, such as hero_list_json
    '''Get a dictionary of hero names corresponding to hero numbers, for example (小乔:106,...)'''
    cname_ename = {}
    for hero in hero_json:
        cname_ename[hero['cname']] = hero['ename']
    return cname_ename

def get_skin_name(hero_json): #pass in the json obtained from the web page to convert the object into a python dictionary
    '''Get a dictionary of all skin names of the skin corresponding to the hero name, for example
    {'Xiao Qiao':'Breeze of Love|Halloween Eve|Swan Dream|Pure White Flower Wedding|Colorful Unicorns',...}'''
    cname_skin_name = {}
    for hero in hero_json:
        cname_skin_name[hero['cname']] = hero['skin_name']
    return cname_skin_name

def get_hero_skin_count(cname_skin_name): #Pass in a dictionary of hero names corresponding to skin names
    '''Get the number of skins corresponding to each hero, for example ('小乔': 5,...)'''
    cname_skin_count = {} 
    for item in cname_skin_name.items():
        cname_skin_count[item[0]] = len(item[1].split('|'))
    return cname_skin_count

def get_skin_name_url(skin_base_rul,cname_skin_count,cname_ename):
    #Pass in the dictionary with the root address and name of the skin corresponding to the number of skins and the dictionary with the number corresponding to the name of the skin
    '''Returns a dictionary of the URL address list of all skins corresponding to the hero name, for example {小乔:[skin_url1,skin_url2],...)'''
    cname_url_list = {}
    for cname,count in cname_skin_count.items():
        base_url = skin_base_rul+str(cname_ename[cname])+'/'+str(cname_ename[cname])+'-bigskin-'
        skin_url_list = [str(base_url)+str(num)+'.jpg' for num in range(1,count+1)]
        cname_url_list[cname] = skin_url_list
    return cname_url_list

d = get_skin_name_url(hero_skin_root_url,get_hero_skin_count(get_skin_name(hero_list_json)),get_ename(hero_list_json))

def get_cname_skin_name(cname_skin_name):#Incoming name corresponds to a dictionary of skin name strings
    cname_skin_name_dict = {} #Returns the dictionary whose name corresponds to [list of skin names]
    for cname,skin_name_list in cname_skin_name.items():
        skin_list = [name for name in skin_name_list.split('|')]
        cname_skin_name_dict[cname] = skin_list
    return cname_skin_name_dict
#s = get_skin_name(hero_list_json)
#f = get_cname_skin_name(s)

def get_hero_skin(cname_url_list,cname_skin_name):#The dictionary whose name corresponds to the [skin name list] and the dictionary whose name corresponds to the skin url list
   # """Get pictures of each hero"""
    for cname,skin_url in cname_url_list.items():
        if mkdir(skin_base_dir+cname):#Create the specified directory
            os.chdir(skin_base_dir+cname) #Enter the created directory
            for i in range(len(skin_url)):
                file_name = cname_skin_name[cname][i]+'.jpg'
                r = requests.get(skin_url[i])
                with open(file_name,'wb') as f:
#Create a directory
def mkdir(path):
    # Import module
    import os
    # Remove the first space
    # Remove the trailing/symbol
    # Determine whether the path exists
    # Existing True
    # Does not exist False
    # critical result
    if not isExists:
        # Create a directory if it does not exist
        # Create directory operation function
        print(path+'created successfully')
        return True
        # If the directory exists, do not create it, and prompt that the directory already exists
        print(path+'directory already exists')
        return False

if __name__ =='__main__':
    hero_list_body = requests.get(hero_list_url) #Request hero list
    hero_list_json = hero_list_body.json() #Convert the acquired json data of the hero list into a python object

    cname_ename = {} #Dictionary of hero name corresponding to hero number
    cname__skin_name = {} #Dictionary of hero name corresponding to skin name string
    cname_skin_count = {} #Dictionary of hero names corresponding to the number of skins
    cname_skin_name_str_list = get_skin_name(hero_list_json)
    cname_skin_name_list = get_cname_skin_name(cname_skin_name_str_list)
    cname_skin_count = get_hero_skin_count(cname_skin_name_str_list)
    cname_ename = get_ename(hero_list_json)
    cnam_skin_url_list = get_skin_name_url(hero_skin_root_url,cname_skin_count,cname_ename)

The following is a sample folder for saving captured pictures:

The above is a simple example of capturing the skins of all heroes of Glory of Kings. The above code does not use python multi-threading to execute the function of capturing pictures, so it may take a few minutes to execute.

Reference: Use Python to complete the download of the full skin of the hero of the glory of the king-Cloud + Community-Tencent Cloud