urllib.request.urlretrieve用法
'''
urllib.request.urltrieve(url,filename=None,reporthook=None,data=None)
url:远程服务器地址(即需要获取数据的地址)
filename:将远程获取的数据存放入的文件名
'''
import urllib.request def callback(blocknum, blocksize, totalsize): ''' :param blocknum: 已下载数据块 :param blocksize: 数据块大小 :param totalsize: 远程文件大小 :return: ''' percent = 100.0*blocknum*blocksize/totalsize if(percent>100): percent = 100 print('%.2f%%' % percent) url = 'http://www.sina.com.cn' local = 'f:\\sina.html' a,b = urllib.request.urlretrieve(url, local, callback) #从远程下载数据 print(a) print(b)
#输出结果 0.00% 1.39% ... 88.90% 94.46% 95.84% 97.23% 98.62% 100.00% #输出a f:\sina.html #输出b Server: nginx Date: Fri, 08 Jun 2018 02:50:40 GMT Content-Type: text/html Content-Length: 589755 Connection: close Last-Modified: Fri, 08 Jun 2018 02:50:01 GMT Vary: Accept-Encoding X-Powered-By: shci_v1.03 Expires: Fri, 08 Jun 2018 02:51:22 GMT Cache-Control: max-age=60 Age: 18 Via: http/1.1 cnc.guangzhou.ha2ts4.59 (ApacheTrafficServer/6.2.1 [cHs f ]) X-Cache: HIT.59 X-Via-CDN: f=edge,s=cnc.guangzhou.ha2ts4.60.nb.sinaedge.com,c=42.49.109.219;f=Edge,s=cnc.guangzhou.ha2ts4.59,c=112.90.6.60 X-Via-Edge: 1528426240834db6d312aee065a707941b2ee
posted on 2018-06-08 10:53 Magic_chao 阅读(...) 评论(...) 编辑 收藏