使用urllib2而不是urllib在python中下载文件

时间:2016-01-16 20:25:30

标签: python urllib2 urllib

我试图下载tarball文件并使用python将其保存在本地。使用urllib非常简单:

import urllib    

urllib2.urlopen(url, 'compressed_file.tar.gz')
tar = tarfile.open('compressed_file.tar.gz')
print tar.getmembers()

所以我的问题非常简单:使用urllib2库实现这一目标的方法是什么?

1 个答案:

答案 0 :(得分:1)

引用docs

  

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])   打开URL网址,可以是字符串或   请求对象。

     

data可以是指定要发送到服务器的其他数据的字符串,如果不需要此类数据,则可以是None

urlopen接口文档中没有任何内容说明,第二个参数是应该写入响应的文件名。

您需要显式写入从响应文件中读取的数据:

r = urllib2.urlopen(url)
CHUNK_SIZE = 1 << 20
with open('compressed_file.tar.gz', 'wb') as f:
    # line belows downloads all file at once to memory, and dumps it to file afterwards
    # f.write(r.read())
    # below is preferable lazy solution - download and write data in chunks
    while True:
        chunk = r.read(CHUNK_SIZE)
        if not chunk:
            break
        f.write(chunk)
相关问题