main.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import logging
  2. import os
  3. import argparse
  4. from datetime import datetime
  5. from comp.comp import get_comp_info
  6. from heros.heroes import get_heroes_info
  7. from minion.minion import get_minion_info
  8. session_id_value = "" # 请替换为实际的sessionid值
  9. csrf_token_value = "" # 请替换为实际的csrftoken值
  10. def main():
  11. parser = argparse.ArgumentParser(description="爬取hsreplay.net酒馆数据")
  12. parser.add_argument("-o",
  13. "--operation",
  14. choices=["comp", "hero", "minion"],
  15. required=True,
  16. help="comp: 流派 hero: 英雄 minion: 随从")
  17. parser.add_argument("-p",
  18. "--percentile",
  19. choices=["ALL", "TOP_20_PERCENT"],
  20. required=True,
  21. help="ALL: 全部玩家 TOP_20_PERCENT: 百分之二十玩家")
  22. parser.add_argument(
  23. "-r",
  24. "--range",
  25. choices=["CURRENT_BATTLEGROUNDS_PATCH", "LAST_7_DAYS"],
  26. required=True,
  27. help="CURRENT_BATTLEGROUNDS_PATCH: 当前版本 LAST_7_DAYS: 最近7天")
  28. parser.add_argument("-s",
  29. "--sessionid",
  30. default="",
  31. help="session_id的值(可选/在main.py中修改)")
  32. parser.add_argument("-c",
  33. "--csrftoken",
  34. default="",
  35. help="csrf_token的值(可选/在main.py中修改)")
  36. args = parser.parse_args()
  37. params = {
  38. "BattlegroundsMMRPercentile": args.percentile,
  39. "BattlegroundsTimeRange": args.range,
  40. }
  41. log = create_logger(
  42. args.operation,
  43. os.path.join("logs/" + args.operation,
  44. datetime.now().strftime("%Y%m%d_%H%M%S") + ".log"))
  45. session_id_value = args.sessionid
  46. csrf_token_value = args.csrftoken
  47. headers = {
  48. "User-Agent":
  49. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
  50. "Referer": "https://hsreplay.net/",
  51. "accept-language": "zh-CN,zh;q=0.9",
  52. "Cookie":
  53. f"sessionid={session_id_value}; csrftoken={csrf_token_value}",
  54. }
  55. if args.operation == "comp":
  56. get_comp_info(params, headers, log)
  57. elif args.operation == "hero":
  58. get_heroes_info(params, headers, log)
  59. elif args.operation == "minion":
  60. get_minion_info(params, headers, log)
  61. def safe_create_directory(directory):
  62. if not os.path.exists(directory):
  63. os.makedirs(directory)
  64. def create_logger(logger_name, file_name):
  65. safe_create_directory(os.path.dirname(file_name))
  66. logger = logging.getLogger(logger_name)
  67. logger.setLevel(logging.INFO)
  68. file_handler = logging.FileHandler(file_name)
  69. formatter = logging.Formatter(
  70. "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
  71. file_handler.setFormatter(formatter)
  72. logger.addHandler(file_handler)
  73. return logger
  74. if __name__ == "__main__":
  75. main()