Shenglian / -WORK_TIP

工作上小技巧
4 stars 1 forks source link

[python] urllib.request.urlretrieve #168

Open Shenglian opened 5 years ago

Shenglian commented 5 years ago
'''
  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)