Use Python's requests third-party module to capture the King's glory to all the heroes ' skins

Source: Internet
Author: User

This article uses Python's third-party module requests to crawl the King to honor all the heroes ' pictures, and to save the pictures as a directory for each hero in the folder, convenient for use as a desktop wallpaper

The following specific code, which has passed the python3.6 test, can be run successfully:

For the Web connection you want to crawl can be found through the King's Honor official website,

1 #-*-coding:utf-8-*-2 """3 Created on Wed Dec 13:49:524 5 @author: Killertwo6 """7 ImportRequests8 ImportOS9Hero_list_url ='Http://pvp.qq.com/web201605/js/herolist.json'TenHero_skin_root_url ='http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/' OneSkin_base_dir ='c:\\users\\lwt27\\pictures\\image\\heroskin\\' A  -  - defGet_ename (Hero_json):#incoming Python object, such as Hero_list_json the     " "gets a dictionary of hero names corresponding to the hero number, for example {Joe Cole: 106,...}" " -Cname_ename = {} -      forHeroinchHero_json: -cname_ename[hero['CNAME']] = hero['ename'] +     returnCname_ename -  + defGet_skin_name (Hero_json):#incoming JSON converted from web page to Python Dictionary object A     " "get a dictionary of all skin names for the name of the skin corresponding to the hero, for example at {' Little Joe ': ' Love Breeze | halloween | Swan Dream | Pure white flowers | Fun Unicorn ',...}" " -Cname_skin_name = {} -      forHeroinchHero_json: -cname_skin_name[hero['CNAME']] = hero['Skin_name'] -     returnCname_skin_name -  in defGet_hero_skin_count (Cname_skin_name):#Dictionary of incoming hero name corresponding to skin name -     " "get the number of skins for each hero, for example {' Little Joe ': 5,...}" " toCname_skin_count = {}  +      forIteminchCname_skin_name.items (): -Cname_skin_count[item[0]] = Len (Item[1].split ('|')) the     returnCname_skin_count *  $ defGet_skin_name_url (skin_base_rul,cname_skin_count,cname_ename):Panax Notoginseng     #Dictionary of the number of the dictionary and name corresponding to the number of skins that are passed in to the skin root address and name -     " "returns a dictionary of URL address lists for all skins corresponding to the hero name, for example {Joe Cole: [Skin_url1,skin_url2],...}" " theCname_url_list = {} +      forCname,countinchCname_skin_count.items (): A         #print (CNAME) the         #print (count) +         #print (Skin_base_rul) -         #print (Cname_ename[cname]) $Base_url = Skin_base_rul+str (Cname_ename[cname]) +'/'+str (Cname_ename[cname]) +'-bigskin-' $         #print (Base_url) -Skin_url_list = [Str (base_url) +str (num) +'. jpg'  forNuminchRange (1,count+1)] -Cname_url_list[cname] =skin_url_list the     returncname_url_list - Wuyi #print () theD =Get_skin_name_url (Hero_skin_root_url,get_hero_skin_count (Get_skin_name (Hero_list_json)), Get_ename (hero_ List_json)) - #print (d) Wu  - defGet_cname_skin_name (Cname_skin_name):#Dictionary of incoming names corresponding to skin name strings AboutCname_skin_name_dict = {}#returns a dictionary with names corresponding to the list of skin names $      forCname,skin_name_listinchCname_skin_name.items (): -skin_list = [Name forNameinchSkin_name_list.split ('|')] -Cname_skin_name_dict[cname] =skin_list -     returncname_skin_name_dict A      + #s = get_skin_name (Hero_list_json) the #print (s) - #f = get_cname_skin_name (s) $ #print (f) the  the defGet_hero_skin (Cname_url_list,cname_skin_name):#Dictionary with the name of the "Skin Name list" corresponding to the name of the skin URL list the    #"" gets the picture of each hero "" " the      forCname,skin_urlinchCname_url_list.items (): -          in         ifmkdir (Skin_base_dir+cname):#Create the specified directory theOs.chdir (Skin_base_dir+cname)#go to the directory you created the              About              forIinchRange (len (skin_url)): thefile_name = cname_skin_name[cname][i]+'. jpg' theR =Requests.get (Skin_url[i]) theWith open (file_name,'WB') as F: + f.write (r.content) - #Create a directory the defmkdir (path):Bayi     #Introducing Modules the     ImportOS the     #Remove First space -Path=Path.strip () -     #remove tail \ Symbol thePath=path.rstrip ("\\") the     #determine if a path exists the     #exists True the     #does not exist False -isexists=os.path.exists (path) the     #Judging Results the     if  notisexists: the         #Create a directory if it does not exist94         #Create a directory action function the os.makedirs (path) the         Print(path+'created successfully') the         returnTrue98     Else: About         #If the directory exists, it is not created, and you are prompted that the directory already exists -         Print(path+'Directory already exists')101         returnFalse102     return 103 104 if __name__=='__main__': the     106Hero_list_body = Requests.get (Hero_list_url)#Request a list of Heroes107Hero_list_json = Hero_list_body.json ()#convert the retrieved JSON data from a hero list to a Python object108 109Cname_ename = {}#A dictionary of hero names corresponding to hero numbers theCname__skin_name = {}#Dictionary of Hero names corresponding to skin name strings111Cname_skin_count = {}#A dictionary of the number of skins corresponding to the name of the hero the     113Cname_skin_name_str_list =get_skin_name (Hero_list_json) theCname_skin_name_list =get_cname_skin_name (cname_skin_name_str_list) theCname_skin_count =Get_hero_skin_count (cname_skin_name_str_list) theCname_ename =get_ename (Hero_list_json)117Cnam_skin_url_list =Get_skin_name_url (hero_skin_root_url,cname_skin_count,cname_ename)118Get_hero_skin (Cnam_skin_url_list,cname_skin_name_list)

Here is a sample folder to save the captured pictures:

The above is a simple example of capturing the glory of all heroes skin, the above code does not use Python multi-threading to perform the function of fetching pictures, so it may take a few minutes to execute,

Later in the improvement, add the use Python multithreading to perform the crawl task.

Use Python's requests third-party module to capture the King's glory to all the heroes ' skins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.