day23-爬取英雄联盟所有英雄皮肤作业

此篇博客介绍了如何使用Python脚本自动化下载英雄联盟的所有英雄皮肤,包括获取英雄ID、解析皮肤数据并下载图片的过程。

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

day23-爬取英雄联盟所有英雄皮肤作业

import requests
import os


def download(hero_name, skin_name, skin_url):
    response = requests.get(skin_url)
    file_type = os.path.splitext(skin_url)[-1]
    with open(f'英雄皮肤/{hero_name}/{skin_name}{file_type}', 'wb') as f:
        f.write(response.content)
    print('下载成功!')


def get_all_hero_skins():
    # 1. 获取所有英雄的英雄id
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    response = requests.get(url)
    result = response.json()
    all_hero_id = [x['heroId'] for x in result['hero']]

    # 2. 获取每个英雄的皮肤数据
    for hero_id in all_hero_id:
        response = requests.get(f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js')
        result = response.json()
        # 获取英雄名
        hero_name = result['hero']['name']
        # 创建英雄名对应的文件夹
        if not os.path.exists(f'英雄皮肤/{hero_name}'):
            os.mkdir(f'英雄皮肤/{hero_name}')

        for skin in result['skins']:
            skin_name = skin['name'].replace('/', ' ')
            skin_url = skin['mainImg']
            if not skin_url:
                skin_url = skin['chromaImg']
            # 下载图片
            download(hero_name, skin_name, skin_url)


if __name__ == '__main__':
    get_all_hero_skins()

    # result = os.path.splitext('https://game.gtimg.cn/images/lol/act/img/abc.jpeg')
    # print(result)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值