0
点赞
收藏
分享

微信扫一扫

Python3(二):调用王者荣耀英雄列表接口爬虫英雄皮肤海报


展示

源码

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() # 英雄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)))

# hero_skin_count = 10
# for i in range(1, hero_skin_count):
# => 以下1行可以替换成以上2行代码
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)


举报

相关推荐

0 条评论