• 【Python应用】爬取LOL皮肤图片(面向过程编程)


    LOL官网英雄资料:https://lol.qq.com/data/info-heros.shtml

    • 分析网站

    找到英雄列表的json文件(hero_list.js),对应的url(重要)。
    在这里插入图片描述
    找到英雄列表的json文件(hero_list.js),对应的url(重要)。点击preview预览窗体,看到里面是json的格式,对应有hero_Id,name。

    • 代码部分

    获取英雄和id

    def get_hero():
        url = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
        res = requests.get(url=url).json()
        for hero in res['hero']:
            hero_id = hero['heroId']
            # skin_url = 'https://game.gtimg.cn/images/lol/act/img/js/hero/'+hero_id+'.js'
            skin_url = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
    

    获取皮肤图片并以英雄名字保存

    def get_link(url):
        res = requests.get(url=url).json()
        for skin in res['skins']:
            info = skin.get('mainImg')
            if not info:
                # 如果不是皮肤,则跳过当前循环
                continue
            item = {}
            item['heroName'] = skin['heroName']
            # 名字中有 /  替换成 _
            item['skinName'] = skin['name'].replace('/', '_')
            item['link'] = skin['mainImg']
    
            # 英雄名字的文件夹
            hero_path = './imgxx/' + item['heroName'] + '/'
            if not os.path.exists(hero_path):
                # 如果文件夹不存在,则创建
                os.makedirs(hero_path)
            res = requests.get(url=item['link'])
            # 以皮肤名命名图片
            with open(hero_path + item['skinName'] + '.jpg', 'wb') as f:
                f.write(res.content)
    

    层层调用,if name == ‘main’: 是以本文件名为主函数,调用本地的函数

    if __name__ == '__main__':
        get_hero()
    

    秀战绩!在这里插入图片描述

    方便测试,全部代码下载:https://download.csdn.net/download/JeeCai/12240570

    一键三连呀!
  • 相关阅读:
    AcWing 276. I-区域
    学习笔记:可持久化线段树(主席树):静态 + 动态
    NOIP2016提高组 天天爱跑步
    AcWing 195. 骑士精神
    标准文档流
    css 盒模型
    css 继承性和层叠性
    css 选择器
    css 引入方式
    html body中的标签2
  • 原文地址:https://www.cnblogs.com/jee-cai/p/14095231.html
Copyright © 2020-2023  润新知