Skip to content

Instantly share code, notes, and snippets.

@YaYaB
Created October 30, 2019 10:30
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 YaYaB/c4abb2ef4c54b3cfd1f007f0b69db88e to your computer and use it in GitHub Desktop.
Save YaYaB/c4abb2ef4c54b3cfd1f007f0b69db88e to your computer and use it in GitHub Desktop.
Download an image from url
import urllib.request
import shutil
def downloader(image_url, basename, index=None):
req = urllib.request.Request(image_url, headers={'User-Agent': 'Mozilla/5.0'})
if index:
full_file_name = basename + '-' + str(index) + '.jpg'
else:
full_file_name = basename + '.jpg'
with urllib.request.urlopen(req) as response, open(full_file_name, 'wb') as out_file:
shutil.copyfileobj(response, out_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment