展示
注意:
接口返回的对象中的皮肤数与实际的皮肤数可能存在不相等
不过不重要,重要的是思路方法


源码
import os
import requests
import shutil
if __name__ == '__main__':
url = 'https://pvp.qq.com/web201605/js/herolist.json'
hero_list = requests.get(url)
hero_list_json = hero_list.json()
print(hero_list_json)
save_path = './images/'
if os.path.exists(save_path):
shutil.rmtree(save_path)
os.mkdir(save_path)
for hero in hero_list_json:
os.mkdir(save_path + hero['cname'])
hero_skin_count = 1
if 'skin_name' in hero:
hero_skin_count = len(hero['skin_name'].split('|'))
else:
hero_skin_count = 1
print('-【%s】%s个皮肤==================' % (hero['cname'], str(hero_skin_count)))
for i in range(1, hero_skin_count + 1):
hero_link = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/%s/%s-bigskin-%s.jpg' % (
hero['ename'], hero['ename'], str(i))
hero_skin = requests.get(hero_link)
if hero_skin.status_code == 200:
print('success: %s %s 请求成功,开始写入' % (hero['cname'], str(i)))
with open(save_path + hero['cname'] + '/' + str(i) + '.jpg', 'wb') as file:
file.write(hero_skin.content)
print('success: %s %s 写入完成' % (hero['cname'], str(i)))
else:
print('\033[1;31;40merror: %s %s 请求失败\033[0m' % (hero['cname'], str(i)))
print(hero_link)