【Python应用】爬取LOL皮肤图片(面向过程编程)
2020-03-25 19:04
281 查看
LOL官网英雄资料:https://lol.qq.com/data/info-heros.shtml
-
分析网站
找到英雄列表的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
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- day22 yield的表达式的应用,面向过程编程,内置函数前几个
- python基础----迭代器、生成器、协程函数及应用(面向过程实例)
- 【深度之眼Python基础+数据科学入门训练营】第六章 函数-面向过程的编程
- python 多个装饰器组合应用,实现面向切面之AOP编程
- python基础之生成器表达式形式、面向过程编程、内置函数部分
- 【Python基础】1.3.1 面向过程编程与面向对象编程(类)实例
- Python基础-面向过程编程实现Linux下cat -rl ‘dir’ |grep ‘keywords’ 功能
- Python基础-面向过程编程实现Linux下cat -rl ‘dir’ |grep ‘keywords’ 功能
- Python__面向过程编程
- 畅游Python 十:编程思想对比:面向过程、函数式、面向对象
- python快速使用(二)python面向过程编程
- python基础---面向过程编程
- python全栈开发-Day11 迭代器、生成器、面向过程编程
- Python基础-面向对象和面向过程编程区别
- 畅游Python 六:基础函数 - 函数式编程和面向过程编程的区别、open函数、lambda表达式、内置参数列表、递归
- python--表达式形式的yield、面向过程编程、内置函数
- Python基础之面向过程编程
- Python练习-面向过程编程-模拟Grep命令
- Python开发基础-Day10生成器表达式形式、面向过程编程、内置函数部分
- 【Python学习日记】面向过程 和 面向对象 编程