天天看點

爬取LOL皮膚圖檔

1.進入LOL官網,讓後進入"遊戲資料"的"資料庫"

2.F12檢查網頁,在netwoork中找到hero_list.js

爬取LOL皮膚圖檔

 可以看到通過Ajax請求,獲得了英雄清單

3.随便點選幾個英雄頭像,然後可以發現圖檔位址規律

https://game.gtimg.cn/images/lol/act/img/skin/big7004.jpg這是妖姬的一個皮膚,

https://game.gtimg.cn/images/lol/act/img/skin/big64005.jpg這是李青的一個皮膚

可以發現big後面跟的是英雄ID和皮膚ID的拼接

4.皮膚ID我們在hero_list.js中就可以找到,至于皮膚ID我們可以設定一個上限值,進行循環測試,一般皮膚都會在25個以内(這裡不包括炫彩皮膚)

5.将位址進行拼接,就可以進行下載下傳了.

下面是源碼,

import json
import os
import requests
import vthread


@vthread.pool(10)
def get_heroSkin(hero):
    skin_url = 'https://game.gtimg.cn/images/lol/act/img/skin/big{}.jpg'#皮膚位址,下面進行拼接
    heroId = hero['heroId']
    name = hero['name']
    alias = hero['alias']
    title = hero['title']
    if not os.path.exists(f'./lol/{name}-{alias}-{title}'):
        os.mkdir(f'./lol/{name}-{alias}-{title}')
    #拼接位址
    for i in range(25):
        if len(str(i)) == 1:
            i = f'00{i}'
        elif len(str(i)) == 2:
            i = f'0{i}'
        number_url = heroId + i
        response = requests.get(skin_url.format(number_url))
        if response.status_code == 200:
            with open(f'./lol/{name}-{alias}-{title}/{i}.jpg', 'wb') as f:
                f.write(response.content)

def get_heroId(url):
    headers = {

        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/79.0.3945.130 Safari/537.36 '
    }
    response = requests.get(url, headers=headers)
    html = response.content.decode('utf-8') # 是将原網頁的utf-8轉化為unicode(解碼)
    #response.encoding = 'utf-8'進行utf-8編碼
    html = json.loads(html)
    heros = html['hero']
    #print(type(html), html['hero'][0])
    for hero in heros:
        get_heroSkin(hero)


def main():
    # 擷取英雄id
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    if not os.path.exists('./lol'):
        os.mkdir('./lol')
    get_heroId(url)



if __name__ == '__main__':
    main()      

其中引入import vthead是使用多線程加速下載下傳.

下面展示一下成果:

爬取LOL皮膚圖檔