Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Today take you to climb the king of Glory full set of skin, blah blah blah blah blah blah, straight to start ~

The development tools

Python version: 3.6.4

Related modules:

Requests module;

Urllib module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required related modules.

Thought analysis

1, open the official King of Glory wallpaper website address: pvp.qq.com/web201605/w…

2. Shortcut key F12, call out the console for packet capture

3. Find the right links and analyze them

4. View the returned data format

5. Parse URL links

6. Check whether the URL content is the required image and discover that it is actually a thumbnail

7. Analyze websites, click on any wallpaper, and view links in a specific format

8. Find the destination address

9. Analyze the differences between target links and thumbnail links

Thumbnails: http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/200 target figure: http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/0Copy the code

As you can see, replacing 200 after the thumbnail address of the specified format with 0 is the actual image of the target

Code implementation

import os, time, requests, json, re
from retrying import retry
from urllib import parse
 
class HonorOfKings:
    ''' This is a main Class, the file contains all documents. One document contains paragraphs that have several sentences It loads the original file  and converts the original file to new content Then the new content will be saved by this class '''
    def __init__(self, save_path='./heros') :
        self.save_path = save_path
        self.time = str(time.time()).split('. ')
        self.url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataTyp e=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=2 67733&iActId=2735&iModuleId=2735&_=%s' % self.time[0]
 
    def hello(self) :
        ''' This is a welcome speech :return: self '''
        print("*" * 50)
        print(' ' * 18 + Honour of Kings Wallpaper Download)
        print(' ' * 5 + 'Felix Date: 2020-05-20 13:14')
        print("*" * 50)
        return self
 
    def run(self) :
        ''' The program entry '''
        print('left' * 20 + 'Format selection:' + 'left' * 20)
        print(2.1024x768 3.1280x720 4.1280x1024 5.1440x900 6.1920x1080 7.1920x1200 8.1920x1440')
        size = input('Please enter the format number you want to download, default 6:')
        size = size if size and int(size) in [1.2.3.4.5.6.7.8] else 6
 
        print('-- Download start... ')
        page = 0
        offset = 0
        total_response = self.request(self.url.format(page)).text
        total_res = json.loads(total_response)
        total_page = --int(total_res['iTotalPages'])
        print('-- total {} pages... ' . format(total_page))
        while True:
            if offset > total_page:
                break
            url = self.url.format(offset)
            response = self.request(url).text
            result = json.loads(response)
            now = 0
            for item in result["List"]:
                now += 1
                hero_name = parse.unquote(item['sProdName']).split(The '-') [0]
                hero_name = re.sub(R '[[] : < > | · @ # $% ^ & ()]'.' ', hero_name)
                print('-- Downloading page {} hero progress {}/{}... ' . format(offset, hero_name, now, len(result["List"])))
                hero_url = parse.unquote(item['sProdImgNo_{}'.format(str(size))])
                save_path = self.save_path + '/' + hero_name
                save_name = save_path + '/' + hero_url.split('/')[-2]
                if not os.path.exists(save_path):
                    os.makedirs(save_path)
                if not os.path.exists(save_name):
                    with open(save_name, 'wb') as f:
                        response_content = self.request(hero_url.replace("/ 200"."/")).content
                        f.write(response_content)
            offset += 1
        print('-- Download complete... ')
 
    @retry(stop_max_attempt_number=3)
    def request(self, url) :
        ''' Send a request :param url: the url of request :param timeout: the time of request :return: the result of request '''
        response = requests.get(url, timeout=10)
        assert response.status_code == 200
        return response
 
if __name__ == "__main__":
    HonorOfKings().hello().run()
Copy the code

This issue complete source code see personal home page profile for access

Code run result