Python视频下载模块

2018-11-22 08:45:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1. #?-*-?coding:?utf-8?-*-??
  2. '''''?
  3. Created?on?2018111?
  4. ? ?
  5. @author:?sdd?
  6. 使用介绍:?
  7. url:需要下载的视频地址?
  8. path:下载视频的路径以及名称?
  9. '''??
  10. ?? ?
  11. import?os??
  12. import?requests??
  13. def?load_video_media(url,path):??
  14. ????try:??
  15. ????????headers={??
  16. ????????????"User-Atgent":"Mozilla/5.0?(Windows?NT?6.2;?WOW64)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Maxthon/4.3.2.1000?Chrome/30.0.1599.101?Safari/537.36"??
  17. ????????????}??
  18. ????????pre_content_length?=?0??
  19. ????????#循环接收视频??
  20. ????????while?True:??
  21. ????????????#若视频存在,则进行断点续传??
  22. ????????????if?os.path.exists(path):??
  23. ????????????????headers['Range']?=?'bytes=%d-'?%?os.path.getsize(path)??
  24. ????????????res=requests.get(url,stream=True,headers=headers)??
  25. ????????????content_length?=?int(res.headers['content-length'])??
  26. ????????????#?若当前报文长度小于前次报文长度,或者已接收文件等于当前报文长度,则可以认为视频接收完成??
  27. ????????????if?content_length?<?pre_content_length?or?(??
  28. ????????????????????os.path.exists(path)?and?os.path.getsize(path)?==?content_length):??
  29. ????????????????break??
  30. ????????????pre_content_length?=?content_length??
  31. ?? ?
  32. ????????????#?写入收到的视频数据??
  33. ????????????with?open(path,?'ab')?as?file:??
  34. ????????????????file.write(res.content)??
  35. ????????????????file.flush()??
  36. ????????????????print('receive?datafile?size?:?%d???total?size:%d'?%?(os.path.getsize(path),?content_length))??
  37. ????except?Exception?as?e:??
  38. ????????print(e)??
  39. if?__name__?==?'__main__':??
  40. ????url="http://gslb.miaopai.com/stream/SYmuHGhSjZ3DyjMuX7GjVoWhVD298CfXr7HI0Q__.mp4"??
  41. ????path=r"D://TTTTT.mp4"??
  42. ????load_video_media(url,?path)??

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python-01

下一篇:Python中列表的深浅拷贝