您的位置:首页 > 编程语言 > Python开发

【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
  • 收藏
  • 分享
  • 文章举报
被大笨猪拱了的大白菜 发布了13 篇原创文章 · 获赞 7 · 访问量 555 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: