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)