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

本文介绍了一款使用Python的requests和jsonpath库实现的英雄联盟(LOL)英雄皮肤图片爬取脚本。该脚本首先从指定URL获取所有英雄的ID,然后遍历每个英雄的皮肤信息,下载并保存皮肤图片到相应英雄文件夹中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述

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)

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

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值