user2891460
user2891460

Reputation:

Downloading content with python using variables

I'm using the following line of code:

  urllib.urlretrieve(images, thisistest)

In images I have the URL of the image I want to download and thisistest contains the location.

The line of could would work if i entered in the data stored in the variables manually but wont work when it isn't.

The line without the variables however would be:

import urllib

urllib.urlretrieve("https://www.google.co.uk/images/srpr/logo11w.png", "C:\temp")

Anyone any idea how I'd do it with the variables?

Error:

Traceback (most recent call last):
File "C:\Python\main.py", line 68, in <module>
main()
File "C:\Python\main.py", line 65, in main
wp_dc.main(url)
File "C:\Python\wp_dc.py", line 69, in main
download_images(url, page)
File "C:\Python\wp_dc.py", line 39, in download_images
urllib.urlretrieve(i, thisistest)
File "C:\Python27\lib\urllib.py", line 94, in urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "C:\Python27\lib\urllib.py", line 228, in retrieve
url = unwrap(toBytes(url))
File "C:\Python27\lib\urllib.py", line 1057, in unwrap
url = url.strip()
AttributeError: 'list' object has no attribute 'strip'

The code

#Finds all images in page
images = re.findall(r'src=".*http\://([^"]+)"', page)
location = os.path.abspath("C:/temp/coursework/")

#Get the filename
#For each in images
for i in images:
    #Set filename equal to the basename of i
    name = os.path.basename(i)

    #Concantenate the save location and filename
    thisistest = os.path.join(location, name)

    #Test the concantenation
    print 'Concantenation test', thisistest

    urllib.urlretrieve(i, thisistest)

Upvotes: 0

Views: 2309

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599778

Your traceback indicates you are not doing what you said you were. This is the relevant code from that trace:

urllib.urlretrieve("images", "thisistest")

You are passing string literals, not variables. You should do what you originally said:

urllib.urlretrieve(images, thisistest)

Edit OK, now this question is about something completely different. The problem is that images is a list, not a string, and you can't pass a list to urlretrieve. I suspect that you think you are doing something to fix this in the loop - but you aren't, you are just defining a local name variable that is then immediately overwritten and never referenced again anyway.

What you probably want to do is to move most of the code inside the loop:

location = os.path.abspath("C:/temp/coursework/")

for i in images:
    name = os.path.basename(i)
    thisistest = os.path.join(location, name)
    urllib.urlretrieve(i, thisistest)

Also note, these are precisely the sorts of problems that your instructor wants you to be solving yourself, as they are fostering the sort of logical thinking that you need in order to learn programming.

Upvotes: 1

Related Questions