python实现英雄联盟信息获取

执行脚本前需要做的操作

  • 需要具备 python 的环境
    具体安装这里不做赘述
  • 判断是否安装 requests 包
    win+R 输入 cmd 打开命令提示符,输入 pip show requests, 如下则表示已具备 requests 包
    pip show
  • 若未安装 requests
    未安装 requests 使用 pip 工具进行安装 pip install requests
    pip install安装完成后开始写代码
import requests
import os


def get(link):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36'
    }

    response = requests.get(link, headers=headers)

    return response


def get_json(link):
    response = requests.get(link)
    return response.json()


def save_data(data, filePath, wType='wb'):
    # 获取文件路径
    path = filePath[: filePath.rfind('/') + 1]

    # 判断文件是否存在,不存在则创建
    if not os.path.exists(path):
        os.makedirs(path)

    # 判断是否以字节模式写入
    if wType.find('b') == 1:
        with open(filePath, wType)as f:
            f.write(data)
            f.flush()
    else:
        with open(filePath, wType, encoding='utf-8')as f:
            f.write(data)
            f.flush()


if __name__ == '__main__':
    # 存储英雄的js地址
    url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
    # 存储英雄皮肤技能的js地址
    hero_info_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/%s.js'  # % hero.get('heroId')

    # 存储英雄头像的地址
    hero_head_url = 'https://game.gtimg.cn/images/lol/act/img/champion/%s.png'  # % hero.get('alias')

    # 获取英雄信息的列表
    heroLists = get_json(url).get('hero')

    print('正在下载')
    # 遍历进行数据爬取
    for hero in heroLists:
        # 英雄ID heroId
        heroId = hero.get('heroId')

        # 英雄名 name 黑暗之女-title 安妮-alias Annie
        name = hero.get("name")
        title = hero.get("title")
        alias = hero.get('alias')

        # 进度打印
        print(f'英雄:{hero.get("name")}-{hero.get("title")}')
        print(f'\t头像:{hero.get("title")}.png')

        # 设置写入路径
        file = './头像/%s.png' % title

        # 获取图片
        headData = get(hero_head_url % alias).content

        # 写入本地
        save_data(headData, file)

        # 英雄语音包 banAudio 禁用时 selectAudio 选取时
        AudioUrls = {
            '禁用时': hero['banAudio'],
            '选取时': hero['selectAudio']
        }

        print(f'\t语音:')
        for t, url in AudioUrls.items():
            print(f'\t\t{t}.mp3')
            # 设置写入路径
            file = './语音/%s/%s.mp3' % (title, t)

            # 获取语音数据
            audioData = get(url).content

            # 写入本地
            save_data(audioData, file)

        # 存储英雄皮肤技能的json数据
        hero_info = get_json(hero_info_url % heroId)

        # 英雄皮肤
        hero_skins_info = hero_info.get('skins')

        # 英雄技能
        hero_spells_info = hero_info.get('spells')

        # 遍历数据
        print(f'\t皮肤:')
        for v in hero_skins_info:
            # 英雄皮肤对应名称 heroName 黑暗之女 - heroTitle 安妮
            hero_skin_name = v.get('name')
            # 英雄加载皮肤的链接
            hero_skin_png_url = v.get('loadingImg')

            print(f'\t\t{hero_skin_name}.png')

            # 设置写入路径
            file = './皮肤/%s/%s.png' % (title, hero_skin_name)

            # 获取图片
            try:
                hero_skin_data = get(hero_skin_png_url).content
            except requests.exceptions.MissingSchema:
                hero_skin_data = get(v.get('chromaImg')).content
            # 写入本地
            save_data(hero_skin_data, file)

        print(f'\t技能:')
        # 按照指定规律进行排序
        sort = ['passive', 'q', 'w', 'e', 'r']

        # 下标计数
        count = 0
        # 初始化下标
        i = 0
        while i < len(hero_spells_info):
            # 获取信息
            info = hero_spells_info[count]

            # 判断是否符合顺序
            if info.get('spellKey') == sort[i]:
                i += 1
                # 获取技能名称
                name = info.get('name')

                # 获取按键
                spellKey = info.get('spellKey').upper()
                spellKey = (len(spellKey) > 1) and '被动' or spellKey

                # 获取图片地址
                pngUrl = info.get('abilityIconPath')

                # 技能介绍
                txt = f"{((len(spellKey) > 1) and '属性' or '按键')}:{spellKey}\n\n技能名称:{name}\n\n{info.get('description')}"

                print(f'\t\t[{spellKey}]{name}.png')

                # 设置写入路径
                pngFile = './技能/%s/[%s]%s.png' % (title, spellKey, name)
                txtFile = './技能/%s/技能描述.txt' % title

                # 获取数据
                headSkill = get(pngUrl).content

                # 写入本地
                save_data(headSkill, pngFile)
                save_data(f"{'*' * 30}\n{txt}\n", txtFile, 'a')

            else:
                # 不符合依次遍历
                count += 1
                # 到达临界值,重新定义为0
                if count == 5:
                    count = 0
        print('\t\t技能描述.txt')



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值