<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python爬取王者榮耀全套皮膚

          共 6699字,需瀏覽 14分鐘

           ·

          2020-12-26 06:28

          來源:http://suo.im/6pj3Zp


          一、分析需要爬取的網(wǎng)站

          ①、打開官方王者榮耀壁紙網(wǎng)站

          • 網(wǎng)站地址:https://pvp.qq.com/web201605/wallpaper.shtml

          ②、快捷鍵F12,調(diào)出控制臺進行抓包

          ③、找到正確的鏈接并分析

          ④、查看返回數(shù)據(jù)格式

          ⑤、解析url鏈接

          ⑥、查看url內(nèi)容是否是所需圖片,發(fā)現(xiàn)其實是縮略圖

          ⑦、那就去分析網(wǎng)站,隨便點開一張壁紙,查看指定格式的鏈接

          ⑧、找到目標地址

          ⑨、分析目標鏈接和縮略圖的鏈接區(qū)別

          • 縮略圖:http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/200

          • 目標圖:http://shp.qpic.cn/ishow/2735090714/1599460171_84828260_8311_sProdImgNo_6.jpg/0

          • 可以知道,將指定格式的縮略圖地址后面200替換成0就是目標真實圖片

          二、爬蟲代碼

          ①、至此,爬蟲分析完成,爬蟲完整代碼如下

          #!/usr/bin/env python# encoding: utf-8'''#-------------------------------------------------------------------#                   CONFIDENTIAL --- CUSTOM STUDIOS#-------------------------------------------------------------------##                   @Project Name : 王者榮耀壁紙下載##                   @File Name    : main.py##                   @Programmer   : Felix##                   @Start Date   : 2020/7/30 14:42##                   @Last Update  : 2020/7/30 14:42##-------------------------------------------------------------------'''import os, time, requests, json, refrom retrying import retryfrom 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&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=%s' % self.time[0]     def hello(self):        '''        This is a welcome speech        :return: self        '''        print("*" * 50)        print(' ' * 18 + '王者榮耀壁紙下載')        print(' ' * 5 + '作者: Felix  Date: 2020-05-20 13:14')        print("*" * 50)        return self     def run(self):        '''        The program entry        '''        print('↓' * 20 + ' 格式選擇: ' + '↓' * 20)        print('1.縮略圖 2.1024x768 3.1280x720 4.1280x1024 5.1440x900 6.1920x1080 7.1920x1200 8.1920x1440')        size = input('請輸入您想下載的格式序號,默認6:')        size = size if size and int(size) in [1,2,3,4,5,6,7,8] else 6         print('---下載開始...')        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('---總共 {} 頁...' . 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('-')[0]                hero_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', hero_name)                print('---正在下載第 {} 頁 {} 英雄 進度{}/{}...' . 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", "/0")).content                        f.write(response_content)            offset += 1        print('---下載完成...')     @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()

          ②、詳細分析鏈接

          • 其實前端發(fā)送的是jsonp請求,這樣的數(shù)據(jù)在python不好處理,因為不是標準的json格式

          • 因為其前面JQuery1710418919222這個字符串,而知道jsonp的請求的都知道,有這個前綴,必然請求鏈接中有相同的callback參數(shù),將其刪除即可

          • 因此我python代碼中是刪除了這個參數(shù)的

          • 這個鏈接還有很多參數(shù),其實我覺得很多都可以刪除,但是我懶得慢慢去試

          • 這個請求鏈接中最重要的一個參數(shù)必然就是頁碼數(shù),也就是page這個參數(shù),iListNum=20&totalpage=0&page={}

          • 上面的三個參數(shù)是可用的,一個是20,指每頁的數(shù)量,totalpage估計沒啥用,page抓包發(fā)現(xiàn)是從0開始的,這個需要注意一下,因為下面代碼需要將總頁數(shù)減1

          self.url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page={}&iOrder=0&iSortNumClose=1&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=%s' % self.time[0]

          ③、格式選擇

          • 開始運行時,讓你選擇想下載格式的序號,為什么是8個格式呢,看原網(wǎng)頁就知道了,8種不同分辨率的

          • 看上面的圖片,縮略圖鏈接有1-8,對應(yīng)了8中分辨率的縮略圖,那么原圖必然也是8種

          • 這里我默認1920*1080的,一般電腦用這個分辨率的都可以

          • 其中1的原圖,你自己試下,其實也是一個縮略圖,所以一般下載選擇2-8

          print('↓' * 20 + ' 格式選擇: ' + '↓' * 20)print('1.縮略圖 2.1024x768 3.1280x720 4.1280x1024 5.1440x900 6.1920x1080 7.1920x1200 8.1920x1440')size = input('請輸入您想下載的格式序號,默認6:')size = size if size and int(size) in [1,2,3,4,5,6,7,8] else 6

          ④、下載代碼分析

          • 第一次請求主要是為了獲取總頁數(shù),但是請求是從0開始為第一頁,所以需要減去1

          • while true中就是開始從0循環(huán)去請求地址,先找到縮略圖地址,然后將縮略圖的地址鏈接200替換成0就是目標圖片地址了

          • 如果名字中有特殊字符,就將其用正則去除,不然可能會影響路徑的查找

          print('---下載開始...')page = 0offset = 0total_response = self.request(self.url.format(page)).texttotal_res = json.loads(total_response)total_page = --int(total_res['iTotalPages'])print('---總共 {} 頁...' . 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('-')[0]        hero_name = re.sub(r'[【】:.<>|·@#$%^&() ]', '', hero_name)        print('---正在下載第 {} 頁 {} 英雄 進度{}/{}...' . 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", "/0")).content                f.write(response_content)    offset += 1print('---下載完成...')

          ⑤、爬蟲運行的結(jié)果,相同名字的放在同一個文件夾下

          戀習Python

          關(guān)注戀習Python,Python都好練
          好文章,我在看??
          瀏覽 20
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  a在线视频中文字幕 | 小说乱伦视频网站 | 97超碰在线免费观看 | 五月天大香蕉婷婷 | 79AV成人无码 |