1''' urllib.request.urltrieve(url,filename=None,reporthook=None,data=None) url:远程服务器地址(即需要获取数据的地址) filename:将远程获取的数据存放入的文件名'''import urllib.request 2def callback(blocknum, blocksize, totalsize): 3 ''' 4 :param blocknum: 已下载数据块 5 :param blocksize: 数据块大小 6 :param totalsize: 远程文件大小 7 :return: 8 ''' 9 percent = 100.0*blocknum*blocksize/totalsize 10 if(percent>100): 11 percent = 100 12 print('%.2f%%' % percent) 13 14url = 'http://www.sina.com.cn' 15local = 'f:\\sina.html' 16a,b = urllib.request.urlretrieve(url, local, callback) #从远程下载数据 17print(a) 18print(b) 19 20#输出结果 210.00% 221.39% 23 24... 25 2688.90% 2794.46% 2895.84% 2997.23% 3098.62% 31100.00% 32#输出a 33f:\sina.html 34#输出b 35Server: nginx 36Date: Fri, 08 Jun 2018 02:50:40 GMT 37Content-Type: text/html 38Content-Length: 589755 39Connection: close 40Last-Modified: Fri, 08 Jun 2018 02:50:01 GMT 41Vary: Accept-Encoding 42X-Powered-By: shci_v1.03 43Expires: Fri, 08 Jun 2018 02:51:22 GMT 44Cache-Control: max-age=60 45Age: 18 46Via: http/1.1 cnc.guangzhou.ha2ts4.59 (ApacheTrafficServer/6.2.1 [cHs f ]) 47X-Cache: HIT.59 48X-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 49X-Via-Edge: 1528426240834db6d312aee065a707941b2ee
urllib.request.urlretrieve用法
Wesley13
2021-10-11
1458 0 0
点赞
收藏
评论区
加载中...