'''
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