import requests import os from crawl_hero import api ''' @Contact : liuyuqi.gov@msn.cn @Time : 2022/03/08 21:12:55 @License : Copyright © 2017-2020 liuyuqi. All Rights Reserved. @Desc : # 2. 编写一个工具(LOL皮肤下载工具) ----封装的思想 # 给用户展示所有的英雄名称和id (在控制台输出 所有英雄的名称和id 每三个为一行) # 用户输入想要的英雄皮肤 (用户输入所需要的英雄id) # 如果有,则进行下载, 判断id是否在英雄id列表中存在 # 如果没有让用户从新输入 y 继续数据皮肤信息 n 输出程序结束 ''' class CrawlHero(): def __init__(self): self.s = requests.Session() self.hero_id_list = [] def getHeroId(self): response = self.s.get(api.hero_list_url).json() hero_list = response["hero"] print("-----------所有英雄id和名字--------------------") input("按任意键继续") index = 1 tmp = "" for hero in hero_list: tmp = tmp + hero["heroId"] + " " + hero["name"] + " " if index % 3 == 0: print(tmp) tmp = "" self.hero_id_list.append(hero["heroId"]) index = index + 1 def getHeroPic(self): while(1): mHeroId = input("用户输入所需要的英雄id:") # 获取所有英雄图片的地址 for hero_id in self.hero_id_list: if mHeroId == hero_id: hero_img_url = api.hero_img_url % (hero_id) skin_list = self.s.get(hero_img_url).json()["skins"] for skin in skin_list: mainImg = skin["mainImg"] heroName = skin["heroName"] name = skin["name"] if not os.path.exists(heroName): os.mkdir(heroName) if mainImg == "": with open(heroName + "/" + name + ".jpg", "wb") as file: file.write(self.s.get( skin["chromaImg"]).content) else: with open(heroName + "/" + name + ".jpg", "wb") as file: file.write(self.s.get(skin["mainImg"]).content) print("下载结束,头像保存至当前目录\r\n\r\n") yn = input("y 继续 n 输出程序结束:") if yn == "n": exit() def getHero(self): self.getHeroId() self.getHeroPic()