程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python download MP4 video

編輯:Python

import os
import time
import requests
from tqdm import tqdm # Progress bar module 
def down_from_url(url, dst):
# Set up stream=True Parameter reads a large file 
response = requests.get(url, stream=True)
# adopt header Of content-length Property to get the total capacity of the file 
file_size = int(response.headers['content-length'])
if os.path.exists(dst):
# Gets the capacity of some files that have been downloaded locally , Easy to download , If it doesn't exist, download it from scratch .
first_byte = os.path.getsize(dst)
else:
first_byte = 0
# Greater than or equal to indicates that the download is complete , Otherwise continue 
if first_byte >= file_size:
return file_size
header = {
"Range": f"bytes={
first_byte}-{
file_size}"}
pbar = tqdm(total=file_size, initial=first_byte, unit='B', unit_scale=True, desc=dst)
req = requests.get(url, headers=header, stream=True)
with open(dst, 'ab') as f:
# Read one at a time 1024 Bytes 
for chunk in req.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pbar.update(1024)
pbar.close()
return file_size
if __name__ == '__main__':
urlList = []
for url in urlList:
# Generate file name based on timestamp 
down_from_url(url, str(time.time()) + ".mp4")

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved