爬取视频
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('下载结束')