编程语言
首页 > 编程语言> > python 抓取视频并显示下载进度

python 抓取视频并显示下载进度

作者:互联网

import os
import requests
from tqdm import tqdm

VIDEO_PATH = r'videos'
def download(url,fname):
    # 用流stream的方式获取url的数据
    resp = requests.get(url, stream=True)
    total = int(resp.headers.get('content-length', 0))
    with open(fname, 'wb') as file, tqdm(
        desc=fname,
        total=total,
        unit='iB',
        unit_scale=True,
        unit_divisor=1024,
    ) as bar:
        for data in resp.iter_content(chunk_size=1024):
            size = file.write(data)
            bar.update(size)


if __name__ == "__main__":
    # 下载文件,并传入文件名
    with open('gezhilundao.txt','r')as f:
        datas = f.readlines()
    for i in datas:
        url = i.strip()
        video_name = url.split('/')[-1]
        video_full_path = os.path.join(VIDEO_PATH,video_name)
        download(url, video_full_path)

 

标签:__,视频,python,resp,抓取,url,video,total,size
来源: https://www.cnblogs.com/lvye001/p/16489241.html