Python爬虫爬取LOL所有英雄皮肤

在这里插入图片描述

import requests
import os
import jsonpath
from urllib.request import urlretrieve

#获取ID
def get_id():
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    headers = {
        'user - agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 81.0.4044.138Safari / 537.36'
    }
    response = requests.get(url=url, headers=headers)
    r = response.json()
    ids = jsonpath.jsonpath(r, '$..heroId')
    print(ids)
    print("英雄的个数为: " + str(len(ids)))
    return ids


#获取皮肤
def get_skins(ids):
    headers = {
        'user - agent': 'Mozilla / 5.0(Windows NT 10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 81.0.4044.138Safari / 537.36'
    }

    for heroId in ids:
        url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/{}.js'.format(heroId)
        response = requests.get(url=url, headers=headers).json()

        skins = response['skins']
        Img = jsonpath.jsonpath(skins, '$..mainImg')
        names = jsonpath.jsonpath(response, '$..name')

        try:
            if not os.path.exists(names[0]):
                os.mkdir(names[0])
            for name, Imgs in zip(names, Img):
                urlretrieve(Imgs,names[0] + '/' + name + '.jpg')
        except:
            pass
        print('<%s>' % names)

id_list = get_id()
get_skins(id_list)

在这里插入图片描述
在这里插入图片描述

人生漫漫其修远兮,网安无止境。
一同前行,加油!

猜你喜欢

转载自blog.csdn.net/qq_45924653/article/details/108120392