作业:练习使用json接口获取图片并分文件夹整理
"""
Author: Sprite
Date: 2022/5/17 17:07
Have a nice day ~~~~~
"""
import os
import requests
from tqdm import tqdm
def make_dir(path):
if not os.path.exists(path):
os.mkdir(path)
def get_resp(url):
headers = {
'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'
}
resp = requests.get(url=url, headers=headers)
return resp
if __name__ == '__main__':
make_dir('英雄联盟皮肤')
heros = get_resp('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js').json()['hero']
exist_heros = os.listdir('英雄联盟皮肤')
for x in tqdm(heros):
name = x['name'] + '+' + x['title']
print(name)
if name not in exist_heros:
make_dir(f'英雄联盟皮肤/{name}')
hero_id = x['heroId']
hero_link = f'https://game.gtimg.cn/images/lol/act/img/js/hero/{hero_id}.js'
skins = get_resp(hero_link).json()['skins']
skins_dict = {x['name']: x['mainImg'] for x in skins if x['mainImg']}
for i in tqdm(skins_dict):
img = get_resp(skins_dict[i]).content
skin_name = i.replace('/', '')
open(f'英雄联盟皮肤/{name}/{skin_name}.jpg', 'wb').write(img)