Using python's requests third-party module to capture the skin of all heroes of King glory

In this paper, we use the third-party module requests of python to crawl the pictures of all heroes who are honored by the king, and save the pictures into a folder according to each hero, which is convenient for desktop wallpaper

The following specific code has passed the python 3.6 test and can be run successfully:

For the web page to be crawled, the connection can be found on the official website of King glory,

  1 # -*- coding: utf-8 -*-
  2 """
  3 Created on Wed Dec 13 13:49:52 2017
  4 
  5 @author:KillerTwo
  6 """
  7 import requests
  8 import os
  9 hero_list_url = 'http://pvp.qq.com/web201605/js/herolist.json'
 10 hero_skin_root_url = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'
 11 skin_base_dir = 'C:\\Users\\lwt27\\Pictures\\image\\heroskin\\'
 12 
 13 
 14 def get_ename(hero_json):#Incoming acquired python Objects, such as hero_list_json
 15     '''Get a dictionary of hero number corresponding to hero name,for example{Little Joe:106,...}'''
 16     cname_ename = {}
 17     for hero in hero_json:
 18         cname_ename[hero['cname']] = hero['ename']
 19     return cname_ename
 20 
 21 def get_skin_name(hero_json): #Incoming from web page json Convert to python Object of dictionary
 22     '''Get the dictionary of all skin names of the skin corresponding to the hero name,for example
 23     {'Little Joe':'Love breeze|All saints Eve|Dream of Swan|Pure white wedding|Colorful Unicorn',...}'''
 24     cname_skin_name = {}
 25     for hero in hero_json:
 26         cname_skin_name[hero['cname']] = hero['skin_name']
 27     return cname_skin_name
 28 
 29 def get_hero_skin_count(cname_skin_name): #Enter the dictionary of skin name corresponding to hero name
 30     '''Get the number of skins corresponding to each hero,for example{'Little Joe':5,...}'''
 31     cname_skin_count = {} 
 32     for item in cname_skin_name.items():
 33         cname_skin_count[item[0]] = len(item[1].split('|'))
 34     return cname_skin_count
 35 
 36 def get_skin_name_url(skin_base_rul,cname_skin_count,cname_ename):
 37     #The dictionary of the number of skin corresponding to the root address and name of the incoming skin and the dictionary of the number corresponding to the name
 38     '''Returns all skin's url Dictionary of address list,for example{Little Joe:[skin_url1,skin_url2],...}'''
 39     cname_url_list = {}
 40     for cname,count in cname_skin_count.items():
 41         #print(cname)
 42         #print(count)
 43         #print(skin_base_rul)
 44         #print(cname_ename[cname])
 45         base_url = skin_base_rul+str(cname_ename[cname])+'/'+str(cname_ename[cname])+'-bigskin-'
 46         #print(base_url)
 47         skin_url_list = [str(base_url)+str(num)+'.jpg' for num in range(1,count+1)]
 48         cname_url_list[cname] = skin_url_list
 49     return cname_url_list
 50 
 51 #print()
 52 d = get_skin_name_url(hero_skin_root_url,get_hero_skin_count(get_skin_name(hero_list_json)),get_ename(hero_list_json))
 53 #print(d)
 54 
 55 def get_cname_skin_name(cname_skin_name):#Dictionary of skin name string for incoming name
 56     cname_skin_name_dict = {}         #Return the dictionary of skin name corresponding to the name
 57     for cname,skin_name_list in cname_skin_name.items():
 58         skin_list = [name for name in skin_name_list.split('|')]
 59         cname_skin_name_dict[cname] = skin_list
 60     return cname_skin_name_dict
 61     
 62 #s = get_skin_name(hero_list_json)
 63 #print(s)
 64 #f = get_cname_skin_name(s)
 65 #print(f)
 66 
 67 def get_hero_skin(cname_url_list,cname_skin_name):#The input name corresponds to the dictionary of [skin name list] and the name corresponds to the skin url Dictionary of lists
 68    # """Get a picture of each hero"""
 69     for cname,skin_url in cname_url_list.items():
 70         
 71         if mkdir(skin_base_dir+cname):#Create the specified directory
 72             os.chdir(skin_base_dir+cname)  #Go to the created directory
 73             
 74             for i in range(len(skin_url)):
 75                 file_name = cname_skin_name[cname][i]+'.jpg'
 76                 r = requests.get(skin_url[i])
 77                 with open(file_name,'wb') as f:
 78                     f.write(r.content)
 79 #Create directory
 80 def mkdir(path):
 81     # Lead in module
 82     import os
 83     # Remove first space
 84     path=path.strip()
 85     # Remove tail \ Symbol
 86     path=path.rstrip("\\")
 87     # Determine whether the path exists
 88     # existence     True
 89     # non-existent   False
 90     isExists=os.path.exists(path)
 91     # Judgment result
 92     if not isExists:
 93         # Create directory if it does not exist
 94         # Create directory operation function
 95         os.makedirs(path)
 96         print(path+' Created successfully')
 97         return True
 98     else:
 99         # Do not create if the directory exists, and prompt that the directory already exists
100         print(path+' directory already exists')
101         return False
102     return 
103 
104 if __name__ == '__main__':
105     
106     hero_list_body = requests.get(hero_list_url) #Request hero list
107     hero_list_json = hero_list_body.json()  #Get the hero list json Data to python object
108 
109     cname_ename = {}       #Dictionary of hero number corresponding to hero name
110     cname__skin_name = {} #Dictionary of skin name string corresponding to hero name
111     cname_skin_count = {} #The dictionary of skin number corresponding to hero name
112     
113     cname_skin_name_str_list = get_skin_name(hero_list_json)
114     cname_skin_name_list = get_cname_skin_name(cname_skin_name_str_list)
115     cname_skin_count = get_hero_skin_count(cname_skin_name_str_list)
116     cname_ename = get_ename(hero_list_json)
117     cnam_skin_url_list = get_skin_name_url(hero_skin_root_url,cname_skin_count,cname_ename)
118     get_hero_skin(cnam_skin_url_list,cname_skin_name_list)

The following is a sample folder to save the captured pictures:

The above is a simple example of grabbing the skin of all heroes of King glory. The above code does not use python multithreading to execute the function of grabbing pictures, so it may take a few minutes to execute,

In the future, we will improve it and add the use of python multithreading to perform the grab task.

Tags: Python JSON

Posted on Sat, 30 May 2020 08:20:43 -0700 by shage