"""
运用所学爬取lol英雄图片
"""
import os
import requests
domain = 'https://game.gtimg.cn/images/lol/act/img/js'
url = domain + '/heroList' + '/hero_list.js?ts=2767301'
# 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js?ts=2767301'
resp = requests.get(url)
resp.close()
for hero in resp.json()['hero']:
skin_url = f"https://game.gtimg.cn/images/lol/act/img/js/hero/{hero['heroId']}.js?ts=2767301"
skin_resp = requests.get(skin_url)
skin_resp.close()
# resp.encoding= 'utf-8'
i = 0
for dic in skin_resp.json()['skins']:
if dic['mainImg'] != '':
skin_dir = f"lolskin/{dic['heroTitle']}"
foder = os.path.exists(skin_dir)
if not foder:
os.makedirs(skin_dir)
with open(f"lolskin/{dic['heroTitle']}/{i}.jpg", mode='wb') as f:
f.write(requests.get(dic['mainImg']).content)
i += 1