Skip to content

Instantly share code, notes, and snippets.

@qinjie
Created August 19, 2022 11:04
Show Gist options
  • Save qinjie/dcd6265f3811eae6eac61f8492f0fe15 to your computer and use it in GitHub Desktop.
Save qinjie/dcd6265f3811eae6eac61f8492f0fe15 to your computer and use it in GitHub Desktop.
Python download file using Requests

Download files using requests library.

Method 1:

import requests

def download_file(url: str)->str:
    local_filename = url.split('/')[-1]
    # 注意传入参数 stream=True
    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): 
                f.write(chunk)
    return local_filename

Method 2: Use Response.raw and shutil.copyfileobj

import requests
import shutil

def download_file(url: str)->str:
    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

If need to add decoding,

response.raw.read = functools.partial(response.raw.read, decode_content=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment