您当前的位置:首页 > 计算机 > 编程开发 > Python

爬取视频

时间:07-10来源:作者:点击数:

爬取视频

import requests

with requests.get(
        'http://vodmpkc0gki.vod.126.net/vodmpkc0gki/MuMUVVF8_137573812_hd.mp4?resId=9fb803d3ba3c797d808fbd783e81cbd5_0_0&sign=ZWtOPwWMAfYdtWVP4PznOydUQUNfLGjrWaGKpn9gpHk%3D',
        stream=True) as r:
    chunk_size = 10240
    content_size = int(r.headers['content-length'])
    print('下载开始')
    with open('./test.mp4', "wb") as f:
        n = 1
        for chunk in r.iter_content(chunk_size=chunk_size):
            loaded = n * 10240.0 / content_size
            f.write(chunk)
            print('已下载{0:%}'.format(loaded))
            n += 1
print('现在完成')
结果:

已下载30.666899%
已下载30.731872%
已下载30.796844%
已下载30.861816%
已下载30.926788%
已下载30.991761%
已下载31.056733%

import requests
import sys

with requests.get(
        'http://online.cdn.qianqian.com/qianqian/info/5012ec935d74a6fb03015d04585dd353.exe',
        stream=True) as r:
    chunk_size = 10240
    content_size = int(r.headers['content-length'])
    print('下载开始')
    with open('./test.mp4', "wb") as f:
        n = 1
        for chunk in r.iter_content(chunk_size=chunk_size):
            loaded = n * 10240.0 / content_size
            f.write(chunk)
            # print('已下载{0:%}'.format(loaded))
            n += 1
            done = int(50 * loaded)
            sys.stdout.write("\r[%s%s] %d%%" % ('█' * done, ' ' * (50 - done), loaded * 100))
            sys.stdout.flush()
print('现在完成')

结果:
[██████████████████████████████████████████████████] 100%现在完成

with requests.get(url, stream=True) as r:
    print('开始下载。。。')
    content_size = int(r.headers['content-length'])
    with open('.\\video\\{}.flv'.format(id), 'wb')as f:
        n = 1
        for i in r.iter_content(chunk_size=1024):
            loaded = n * 1024.0 / content_size
            # print('\r {}'.format(loaded), end="")
            f.write(i)
            print('\r已下载{0:%}'.format(loaded), end="")
            n += 1
print('下载结束')
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门