pythondownloadurlopenshutilwebimage

Downloading web image w/ urlopen/shutil : error __exit__


I'm trying to download images with shutil/urlopen because of deprecated? I'm not sure if its deprecation, but urlretrieve doesn't download the file, it just creates folder of the image name instead. After looking at other question I saw one that provides this code, but I get an error on this one too.

from urllib2 import urlopen
from shutil import copyfileobj


url = 'http://www.watchcartoononline.com/thumbs/South-Park-Season-14-Episode-11-Coon-2-Hindsight.jpg'
path = 'image.jpg'

with urlopen(url) as in_stream, open(path, 'wb') as out_file:
    copyfileobj(in_stream, out_file)

output

with urlopen(url) as in_stream, open(path, 'wb') as out_file:
AttributeError: addinfourl instance has no attribute '__exit__

Solution

  • Try this:

    import urllib
    urllib.urlretrieve("http://url/img.jpg", "img.jpg")