getHero.py 2.4 KB

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