Python爬虫下载视频文件部分源码

Python爬虫下载视频文件部分源码

 

 

分享一下我自己整理的代码,改两个参数就可使用(扫描下方二维码获取python学习资料)

import requests
import time        
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
}
movie_url = "在这里输入视频地址,例如:http://www.***********.mp4"
movie_name = "视频标题"
downsize = 0
print("开始下载")
startTime = time.time()
req = requests.get(movie_url, headers=headers, stream=True, verify=False)  
      with(open(movie_name+".mp4", "wb")) as f:
          for chunk in req.iter_content(chunk_size=10000):  
              if chunk:
                  f.write(chunk)
                  downsize += len(chunk)
                  line = "downloading %d KB/s - %.2f MB, 共 %.2f MB"
                  line = line % (
                    downsize / 1024 / (time.time() - startTime), downsize / 1024 / 1024, downsize / 1024 / 1024)
                  print(line)