download_video.py 795 B

123456789101112131415161718192021222324
  1. import time
  2. import requests
  3. class DownLoad:
  4. @classmethod
  5. def download_video(cls, video_url, video_path_url):
  6. try:
  7. for i in range(3):
  8. payload = {}
  9. headers = {}
  10. response = requests.request("GET", video_url, headers=headers, data=payload, timeout=50)
  11. if response.status_code == 200:
  12. # 以二进制写入模式打开文件
  13. video = video_path_url + 'video.mp4'
  14. with open(f"{video}", "wb") as file:
  15. # 将响应内容写入文件
  16. file.write(response.content)
  17. time.sleep(5)
  18. return video
  19. return None
  20. except Exception:
  21. return None