方法一:
# -*- coding: utf-8 -*-
import requests
from contextlib import closing
import time
# url-需要下载的视频地址
# path-视频存放位置
def download_file(url, path):
with closing(requests.get(url, stream=True)) as r:
chunk_size = 1024
content_size = int(r.headers['content-length'])
print '下载开始'
with open(path, "wb") as f:
n = 1
for chunk in r.iter_content(chunk_size=chunk_size):
loaded = n*1024.0/content_size
f.write(chunk)
# print '已下载{0:%}'.format(loaded)
n += 1
url = "http://media.cugbonline.cn/dest/cd7/cd7f62da-c976-4163-8494-630e430bb45a.mp4"
download_file(url, "F:\\LearnEng10.mp4")
方法二:
#! /usr/bin/env python
# -*- encoding: UTF-8 -*-
import sys,os
import urllib
def urlcallback(a,b,c):
"""
call back function
a,已下载的数据块
b,数据块的大小
c,远程文件的大小
"""
print "callback"
prec=100.0*a*b/c
print "%.2f%%"%(prec,)
def main(argv):
print "start..."
# urllib.urlretrieve("https://96.f.1ting.com/5d28298b/f44703371f3a2beefee1f445d4202a69/2019/06/05X/05e_Youxiu/01.mp3","F:\\33.mp3",urlcallback)
# urllib.urlretrieve("http://media.cugbonline.cn/dest/cd7/cd7f62da-c976-4163-8494-630e430bb45a.mp4","F:\\34.mp4")
urllib.urlretrieve("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1563531213&di=6a58d83448f9023e9a522d7733c3553b&imgtype=jpg&er=1&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F3ac79f3df8dcd1004cff01c8738b4710b8122ffc.jpg","F:\\34.jpg")
print "end..."
if __name__=="__main__":
main(sys.argv[1:])
网友评论