crawl_hero.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import requests
  2. import os
  3. from crawl_hero import api
  4. '''
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2022/03/08 21:12:55
  7. @License : Copyright © 2017-2020 liuyuqi. All Rights Reserved.
  8. @Desc :
  9. # 2. 编写一个工具(LOL皮肤下载工具) ----封装的思想
  10. # 给用户展示所有的英雄名称和id (在控制台输出 所有英雄的名称和id 每三个为一行)
  11. # 用户输入想要的英雄皮肤 (用户输入所需要的英雄id)
  12. # 如果有,则进行下载, 判断id是否在英雄id列表中存在
  13. # 如果没有让用户从新输入 y 继续数据皮肤信息 n 输出程序结束
  14. '''
  15. class CrawlHero():
  16. def __init__(self):
  17. self.s = requests.Session()
  18. self.hero_id_list = []
  19. def getHeroId(self):
  20. response = self.s.get(api.hero_list_url).json()
  21. hero_list = response["hero"]
  22. print("-----------所有英雄id和名字--------------------")
  23. input("按任意键继续")
  24. index = 1
  25. tmp = ""
  26. for hero in hero_list:
  27. tmp = tmp + hero["heroId"] + " " + hero["name"] + " "
  28. if index % 3 == 0:
  29. print(tmp)
  30. tmp = ""
  31. self.hero_id_list.append(hero["heroId"])
  32. index = index + 1
  33. def getHeroPic(self):
  34. while(1):
  35. mHeroId = input("用户输入所需要的英雄id:")
  36. # 获取所有英雄图片的地址
  37. for hero_id in self.hero_id_list:
  38. if mHeroId == hero_id:
  39. hero_img_url = api.hero_img_url % (hero_id)
  40. skin_list = self.s.get(hero_img_url).json()["skins"]
  41. for skin in skin_list:
  42. mainImg = skin["mainImg"]
  43. heroName = skin["heroName"]
  44. name = skin["name"]
  45. if not os.path.exists(heroName):
  46. os.mkdir(heroName)
  47. if mainImg == "":
  48. with open(heroName + "/" + name + ".jpg", "wb") as file:
  49. file.write(self.s.get(
  50. skin["chromaImg"]).content)
  51. else:
  52. with open(heroName + "/" + name + ".jpg", "wb") as file:
  53. file.write(self.s.get(skin["mainImg"]).content)
  54. print("下载结束,头像保存至当前目录\r\n\r\n")
  55. yn = input("y 继续 n 输出程序结束:")
  56. if yn == "n":
  57. exit()
  58. def getHero(self):
  59. self.getHeroId()
  60. self.getHeroPic()