标签:while 下载 res .net code 实现 文件 str under
Requests库,高度封装的http库
import requests url = ‘http://down.sandai.net/thunder9/Thunder9.0.18.448.exe‘ filename = url.split(‘/‘)[-1] #获取文件名 r = requests.get(url,stream = True) with open(filename,‘wb‘) as f: p = 0 #下载计数器 chunk_size = 4096 #块大小 try: while True: for data in r.iter_content(chunk_size): p += f.write(data) print(‘%d‘ % (p)) except Exception as e: print(e) finally: print(‘下载完毕!‘)
urllib库,操作上能比Requests灵活一点,没有特殊需求的话基本没什么差别
import urllib.request url = ‘http://down.sandai.net/thunder9/Thunder9.0.18.448.exe‘ resp = urllib.request.urlopen(url) filename = url.split(‘/‘)[-1] with open(filename,‘wb‘) as f: p = 0 #下载计数 buffsize = 4096 #块大小 try: while True: buff = resp.read(buffsize) if not buff: #buff为空,即下载完毕,结束循环 break p += f.write(buff) print(‘%d‘ % p) except Exception as e: print(e) finally: print(‘下载完毕!‘)
还有个一句话的
import urllib urllib.urlretrieve(url, ‘test.jpg’)
标签:while 下载 res .net code 实现 文件 str under
原文地址:http://www.cnblogs.com/vawter/p/6035172.html