使用requests在python中下载大文件

2024-05-16 02:42:20 发布

您现在位置:Python中文网/ 问答频道 /正文

Requests是一个非常好的库。我想用它来下载大文件(>;1GB)。 问题是无法将整个文件保存在内存中,我需要将其分块读取。下面的代码有问题

import requests

def DownloadFile(url)
    local_filename = url.split('/')[-1]
    r = requests.get(url)
    f = open(local_filename, 'wb')
    for chunk in r.iter_content(chunk_size=512 * 1024): 
        if chunk: # filter out keep-alive new chunks
            f.write(chunk)
    f.close()
    return 

因为某种原因它不能这样工作。它仍然会在将响应保存到文件之前将其加载到内存中。

更新

如果您需要一个小客户机(Python 2.x/3.x),它可以从FTP下载大文件,您可以找到它here。它支持多线程重新连接(它监视连接),还为下载任务调整套接字参数。


Tags: 文件内存代码importgturllocaldef
3条回答

使用以下流代码,无论下载文件的大小如何,Python内存使用都会受到限制:

def download_file(url):
    local_filename = url.split('/')[-1]
    # NOTE the stream=True parameter below
    with requests.get(url, stream=True) as r:
        r.raise_for_status()
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=8192): 
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)
                    # f.flush()
    return local_filename

注意,使用iter_content返回的字节数并不完全是chunk_size;它应该是一个通常大得多的随机数,并且在每次迭代中都应该是不同的。

请参阅http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow以获取进一步的参考。

使用^{}^{}会容易得多:

import requests
import shutil

def download_file(url):
    local_filename = url.split('/')[-1]
    with requests.get(url, stream=True) as r:
        with open(local_filename, 'wb') as f:
            shutil.copyfileobj(r.raw, f)

    return local_filename

这将文件流式传输到磁盘而不使用过多的内存,并且代码很简单。

不完全是OP的要求,但是。。。用urllib很容易做到这一点:

from urllib.request import urlretrieve
url = 'http://mirror.pnl.gov/releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso'
dst = 'ubuntu-16.04.2-desktop-amd64.iso'
urlretrieve(url, dst)

或者这样,如果要将其保存到临时文件中:

from urllib.request import urlopen
from shutil import copyfileobj
from tempfile import NamedTemporaryFile
url = 'http://mirror.pnl.gov/releases/16.04.2/ubuntu-16.04.2-desktop-amd64.iso'
with urlopen(url) as fsrc, NamedTemporaryFile(delete=False) as fdst:
    copyfileobj(fsrc, fdst)

我观察了这个过程:

watch 'ps -p 18647 -o pid,ppid,pmem,rsz,vsz,comm,args; ls -al *.iso'

我看到文件在增长,但内存使用量保持在17MB。我遗漏了什么吗?

相关问题 更多 >