import jsonpath import requests import json import os import time # 程序开始时间 start = time.time() from tqdm import tqdm from time import sleep # 如果有英雄联盟就存入没有就创建这个文件夹 yxlm_hero_skin_dir = 'D:/Python2020-邱勋涛/爬虫self/英雄联盟/' if not os.path.exists(yxlm_hero_skin_dir): os.mkdir(yxlm_hero_skin_dir) # hero_list.js文件 url1 = 'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js' hero_text = requests.get(url1).text # 转为 json 格式,且去掉hero成列表形式 hero_json = json.loads(hero_text)['hero'] # 横向皮肤的张数 x = 0 # 竖向皮肤的张数 q = 0 # 炫彩皮肤的张数 t = 0 for m in range(len(hero_json)): # 英雄id hero_id = hero_json[m]["heroId"] # 英雄名字 hero_name = hero_json[m]['name'] # print(hero_id) # print(hero_name) url2 = ("https://game.gtimg.cn/images/lol/act/img/js/hero/%s.js") % hero_id hero_text = requests.get(url2).text # 获取皮肤的js文件 从而获取他的皮肤id every_hero_js = json.loads(hero_text)["skins"] # print(len(every_hero_js)) # print(every_hero_js) # 低奢的一种进度条显示的方法,就是利用tqdm库来实现 for i in tqdm(range(len(every_hero_js))): heroName = every_hero_js[i]["name"] # 因为文件夹和图片命名不能带 / ,K/DA系列 的皮肤需要进行判断,存在替换成空 if "/" in heroName: heroName = heroName.replace("/", "") print(heroName) # 皮肤id skin_Id = every_hero_js[i]["skinId"] print(skin_Id) # 横向皮肤的url across_pic_url = every_hero_js[i]["mainImg"] # 如果url为空,跳过不下载 if not across_pic_url: across_pic_url = None print(across_pic_url) # 竖向皮肤的url vertical_pic_url = every_hero_js[i]["loadingImg"] # 如果url为空,跳过不下载 if not vertical_pic_url: vertical_pic_url = None print(vertical_pic_url) # 炫彩皮肤的url xuanchai_skin_url = every_hero_js[i]["chromaImg"] # 如果url为空,跳过不下载 if not xuanchai_skin_url: xuanchai_skin_url = None print(xuanchai_skin_url) # 分类文件夹 hero_skin_dir = yxlm_hero_skin_dir + hero_name + "/" if not os.path.exists(hero_skin_dir): os.mkdir(hero_skin_dir) # 如果url存在 则保存图片 if across_pic_url: skin_a_picture = requests.get(across_pic_url).content with open(hero_skin_dir + heroName + '.jpg', 'wb') as f: f.write(skin_a_picture) x = x + 1 # 如果url存在 则保存图片 if vertical_pic_url: skin_v_picture = requests.get(vertical_pic_url).content with open(hero_skin_dir + heroName + '.png', 'wb') as f: f.write(skin_v_picture) q = q + 1 # 如果url存在 则保存图片 if xuanchai_skin_url: skin_c_picture = requests.get(xuanchai_skin_url).content with open(hero_skin_dir + heroName + '.png', 'wb') as f: f.write(skin_c_picture) t = t + 1 # 计算皮肤总数量 z = t + q + x # 程序结束时间 end = time.time() # 计算执行时间 over_time = end - start print("本次服务共下载%s张英雄联盟皮肤图片,一共耗时%s秒,欢迎下次光临" % (z, over_time)) # 邱勋涛撰写,未经允许,不得转载,否则坐牢吧 # 直接复制代码运行,需要修改代码中的俩路径,别这么懒 # 这里面注释很详细,认真看能看懂,不懂就问同学