Search code examples
python-2.7urlliburlretrieve

Python2.7 how to loop urllib to download images


I have a list of urls of some images and I want to download them import urllib

links =  ['http://www.takamine.com/templates/default/images/gclassical.png',
 'https://dk1xgl0d43mu1.cloudfront.net/user_files/esp/product_images/000/010/120/large.png?1389980652',
'https://dk1xgl0d43mu1.cloudfront.net/user_files/esp/product_images/000/020/676/large.png?1453396324']

#urllib.urlretrieve('http://www.takamine.com/templates/default/images/gclassical.png','image.jpg')
for i in range(0,4):
    S1 = 'image'
    S2 = '.png'
    name = list()
    x = S1 + str(i) + S2
    name.append(x)

for q in links:
    urllib.urlretrieve(q,name)

I understand how to retrieve one at a time....when I try this code i get this error

Traceback (most recent call last): File "C:/Python27/metal memes/test1.py", line 17, in urllib.urlretrieve(q,name) File "C:\Python27\lib\urllib.py", line 98, in urlretrieve return opener.retrieve(url, filename, reporthook, data) File "C:\Python27\lib\urllib.py", line 249, in retrieve tfp = open(filename, 'wb') TypeError: coercing to Unicode: need string or buffer, list found

any answers , explanations are appreciated


Solution

  • The first for loop is there to create a list of filenames image0.png to image3.png, right?! That fails and produces a list of only one element ('image3.png') because you reinitialize the list inside the loop. You have to initialize it once before the loop. You can check this easily if you put a print name after the loop

    The second problem is, that you pass a list to urllib.urlretrieve Your question is not clear in this regard, but do you want to download 4 images named image0.png...image3.png from every of the given urls? That's how your code looks like.

    If yes, you need a nested loop over the names in the list of filenames. I modified the code below accordingly. But your urls already contain a filename, so I'm not sure what's the real intension.

    links =  ['http://www.takamine.com/templates/default/images/gclassical.png',
     'https://dk1xgl0d43mu1.cloudfront.net/user_files/esp/product_images/000/010/120/large.png?1389980652',
    'https://dk1xgl0d43mu1.cloudfront.net/user_files/esp/product_images/000/020/676/large.png?1453396324']
    
    #urllib.urlretrieve('http://www.takamine.com/templates/default/images/gclassical.png','image.jpg')
    
    # create a list of filenames
    # either this code:
    names = list()
    for i in range(0,4):
        S1 = 'image'
        S2 = '.png'
        x = S1 + str(i) + S2
        names.append(x)
    
    # or, as suggested in the comments, much shorter using list comprehension:
    names = ["image{}.png".format(x) for x in range(4)]
    
    for q in links:
        for name in names:
            urllib.urlretrieve(q,name)