GetVideoInfo.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. # @Author : ChenZhaoyuchen
  3. # @Time : 2024/9/13 15:02
  4. # @File : GetVideoInfo.py
  5. import json
  6. import re
  7. from utils.GetResponse import *
  8. from pprint import pprint
  9. video_json = {
  10. }
  11. def GetVideoInfo() -> str:
  12. response_video = GetResponse_AV()
  13. html_video = response_video.text
  14. info_video = re.findall('<script>window.__playinfo__=(.*?)</script>', html_video)[0]
  15. json_data_video = json.loads(info_video)
  16. video_url = json_data_video['data']['dash']['video'][0]['baseUrl']
  17. return video_url
  18. def GetAudioInfo() -> str:
  19. response_audio = GetResponse_AV()
  20. html_audio = response_audio.text
  21. info_audio = re.findall('<script>window.__playinfo__=(.*?)</script>', html_audio)[0]
  22. json_data_audio = json.loads(info_audio)
  23. audio_url = json_data_audio['data']['dash']['audio'][0]['baseUrl']
  24. return audio_url
  25. def GetTitile():
  26. response_audio = GetResponse_AV()
  27. html = response_audio.text
  28. title = re.findall('<title data-vue-meta="true">(.*?)</title>', html)[0]
  29. print("原名为:",title)
  30. video_json[title] = title
  31. # if not title:
  32. # title = '未知'
  33. if title:
  34. illegal_chars = fr'<|>\/:"*?'
  35. def remove_illegal_chars(title_ill):
  36. for char in illegal_chars:
  37. title_ill = title_ill.replace(char, "")
  38. return title_ill
  39. title = remove_illegal_chars(title)
  40. else:
  41. return None
  42. print('文件名为:',title)
  43. # elif len(title) > 30:
  44. # # 如果名字过长,就取前20对反爬虫策略有一定的反制手段,如使用代理IP、设置随机访问时间、获取ajax等个字符
  45. # title = title[:30]
  46. # return title
  47. # 仅用作测试
  48. def GetHTML():
  49. response = GetResponse_AV()
  50. html = response.text
  51. return html
  52. # # 测试代码
  53. if __name__ == '__main__':
  54. A = GetAudioInfo()
  55. print(A)
  56. B = GetVideoInfo()
  57. print(B)
  58. C = GetTitile()
  59. print('修饰过后名字为:',C)
  60. D = GetHTML()
  61. pprint(D)