get_url.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. # @Author: wangkun
  3. # @Time: 2022/9/1
  4. import json
  5. import os
  6. import sys
  7. import time
  8. sys.path.append(os.getcwd())
  9. from crawler_shipinhao.main.common import Common
  10. class GetUrl:
  11. @classmethod
  12. def get_url(cls, log_type):
  13. try:
  14. # charles 抓包文件保存目录
  15. charles_file_dir = r"./crawler_kanyikan_recommend/chlsfiles/"
  16. if int(len(os.listdir(charles_file_dir))) == 1:
  17. Common.logger(log_type).info("未找到chlsfile文件,等待60s")
  18. time.sleep(60)
  19. else:
  20. # 目标文件夹下所有文件
  21. all_file = sorted(os.listdir(charles_file_dir))
  22. # 获取到目标文件
  23. old_file = all_file[-1]
  24. # 分离文件名与扩展名
  25. new_file = os.path.splitext(old_file)
  26. # 重命名文件后缀
  27. os.rename(os.path.join(charles_file_dir, old_file),
  28. os.path.join(charles_file_dir, new_file[0] + ".txt"))
  29. with open(charles_file_dir + new_file[0] + ".txt", encoding='utf-8-sig', errors='ignore') as f:
  30. contents = json.load(f, strict=False)
  31. video_url_list = []
  32. cover_url_list = []
  33. if "finder.video.qq.com" in [text['host'] for text in contents]:
  34. for text in contents:
  35. if text["host"] == "finder.video.qq.com" and text["path"] == "/251/20302/stodownload":
  36. video_url_list.append(text)
  37. elif text["host"] == "finder.video.qq.com" and text["path"] == "/251/20350/stodownload":
  38. cover_url_list.append(text)
  39. video_url = video_url_list[0]['host']+video_url_list[0]['path']+'?'+video_url_list[0]['query']
  40. cover_url = cover_url_list[0]['host']+cover_url_list[0]['path']+'?'+cover_url_list[0]['query']
  41. return video_url, cover_url
  42. else:
  43. Common.logger(log_type).info("未找到 url,10s后重新获取")
  44. time.sleep(10)
  45. cls.get_url(log_type)
  46. except Exception as e:
  47. Common.logger(log_type).exception("get_url异常:{}", e)
  48. return None
  49. if __name__ == '__main__':
  50. GetUrl.get_url('recommend')