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

Download a file in python with urllib2 instead of urllib

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

import urllib    

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

所以我的问题很简单:使用 urllib2 库实现这个的方法是什么?

引用 docs:

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]]) Open the URL url, which can be either a string or a Request object.

data may be a string specifying additional data to send to the server, or None if no such data is needed.

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)