Skip to content

Instantly share code, notes, and snippets.

@kernel1994
Created January 15, 2019 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernel1994/3d3c3fba8107615f417cc3dbcf18fb74 to your computer and use it in GitHub Desktop.
Save kernel1994/3d3c3fba8107615f417cc3dbcf18fb74 to your computer and use it in GitHub Desktop.
download image via requests or urllib
import shutil
import urllib
import pathlib
import requests
from PIL import Image
from io import BytesIO
def get_file_name(url: str) -> str:
return url.split('/')[-1].split('?')[0]
def urllib_urlretrieve(url: str):
file_name = 'urllib_urlretrieve_' + get_file_name(url)
file_path = pathlib.Path(file_name)
urllib.request.urlretrieve(url, file_path)
def requests_1(url: str):
file_name = 'requests_1_' + get_file_name(url)
file_path = pathlib.Path(file_name)
# `Response.raw` is a raw stream of bytes – it does not transform the response content.
# If you really need access to the bytes as they were returned, use `Response.raw`.
# Must set `stream=True` in your initial request.
r = requests.get(url, stream=True)
with file_path.open('wb') as o:
shutil.copyfileobj(r.raw, o)
def requests_2(url: str):
file_name = 'requests_2_' + get_file_name(url)
file_path = pathlib.Path(file_name)
# use `r.content` access the response body as bytes, for non-text requests
# The gzip and deflate transfer-encodings are automatically decoded for you.
r = requests.get(url)
with file_path.open('wb') as o:
o.write(r.content)
def requests_chunk(url: str):
file_name = 'requests_chunk_' + get_file_name(url)
file_path = pathlib.Path(file_name)
# get raw socket response from the server by using `r.raw`
# must set `stream=True` in your initial request.
r = requests.get(url, stream=True)
with file_path.open('wb') as o:
# `Response.iter_content` will automatically decode the gzip and deflate transfer-encodings.
for chunk in r.iter_content(chunk_size=32):
o.write(chunk)
def requests_pil(url: str):
file_name = 'requests_pil_' + get_file_name(url)
file_path = pathlib.Path(file_name)
r = requests.get(url)
image = Image.open(BytesIO(r.content))
image.save(file_path)
if __name__ == '__main__':
url = 'http://docs.python-requests.org/en/master/_static/requests-sidebar.png'
urllib_urlretrieve(url)
requests_1(url)
requests_2(url)
requests_chunk(url)
requests_pil(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment