How to download images from url in Python

Post date: Jan 10, 2016 7:33:02 AM

There are at least two ways to do so. Here they are:

1) use os.system() to call "wget" command line. I answered the question here.

import os out_image = 'out_005.png' url = 'http://mangadoom.co/wp-content/manga/5170/886/005.png' os.system("wget -O {0} {1}".format(out_image, url))

2) Use combination of the packages requests and shutil, see example.

import requests import shutil  r = requests.get('http://mangadoom.co/wp-content/manga/5170/886/005.png', stream=True) if r.status_code == 200:     with open("img.png", 'wb') as f:         r.raw.decode_content = True         shutil.copyfileobj(r.raw, f)