import os
import time
import requests
heroUrl = 'https://pvp.qq.com/web201605/js/herolist.json'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/78.0.3904.70 Safari/537.36'}
# 创建目录文件
def makedir(x):
# 判断目录是否存在,如不存在则创建
if not os.path.exists(x):
os.makedirs(x)
pic = "D:/test/pic"
skin_count_sum = 0
hero_count_sum = 0
startTime = time.time()
try:
response = requests.get(heroUrl, headers=headers)
json_list = response.json()
hero_count_sum = len(json_list)
for m in range(len(json_list)):
# 编号
ename = json_list[m]['ename']
# 名称
cname = json_list[m]['cname']
# 皮肤数量
skin_count = 0
# 判断是否有皮肤
if json_list[m].__contains__('skin_name'):
# 皮肤列表json
skin_name = json_list[m]['skin_name'].split('|')
# 皮肤数量
skin_count = len(skin_name)
# 打印英雄的皮肤数量
print("英雄编号:", ename, ' 英雄名称:', cname, ' 英雄皮肤数量:', skin_count)
hero_name_pic = pic + "/" + str(ename) + "-" + cname + "-" + str(skin_count) + "/"
makedir(hero_name_pic)
# 遍历皮肤
for i in range(1, skin_count + 1):
# 网址拼接, 构造完整的图片网址(可在页面请求是否有响应)
# https://game.gtimg.cn/images/yxzj/img201606/heroimg/525/525-bigskin-1.jpg
url = 'http://game.gtimg.cn/images/yxzj/img201606/heroimg/' # 图片网址固定前缀
url_pic = url + str(ename) + '/' + str(ename) + '-bigskin-' + str(i) + '.jpg'
# 请求获取图片信息
picture = requests.get(url_pic).content
# 写入文件
with open(hero_name_pic + cname + ' - ' + skin_name[i - 1] + '.jpg', 'wb') as f:
f.write(picture)
else:
# 打印英雄的皮肤数量
print("英雄编号:", ename, ' 英雄名称:', cname, ' 英雄皮肤数量:', skin_count)
hero_name_pic = pic + "/" + str(ename) + "-" + cname + "-" + str(skin_count) + "/"
makedir(hero_name_pic)
skin_count_sum += skin_count
except KeyError as e:
print("异常:%s" % e)
print('程序执行完毕')
print("英雄总计:%s" % hero_count_sum)
print("皮肤总计:%s" % skin_count_sum)
endTime = time.time()
print('本次图片总共耗时 %s s' % round((endTime - startTime), 2))
exit(65536)
说明:在上述heroUrl请求中获取的herolist,缺少部分皮肤的数据统计,导致无法获取全皮肤数量,
如 英雄“马超”等,需要自己核对具体数值
参考:https://blog.csdn.net/WeiLanooo/article/details/100547708