0

Using a list I am able to get all url's from a webage already into list imgs_urls. I need to now how to save all images from a webage, with the number of images changing.

Within the imgs_urls list depending on what report I run, there can be any number of urls in the list. This currently already works by calling just one list item.

html = lxml.html.fromstring(data)
imgs = html.cssselect('img.graph')
imgs_urls = []
for x in imgs:
   imgs_urls.append('http://statseeker%s' % (x.attrib['src']))

lnum = len(imgs_urls)
link = urllib2.Request(imgs_urls[0])
output = open('sla1.jpg','wb')
response = urllib2.urlopen(link)
output.write(response.read())
output.close()

The urls in the lsit are full urls. This list would readback something like this if printed:

img_urls = ['http://site/2C2302.png','http://site/2C22101.png','http://site/2C2234.png']

Basic premise of what I think something like this would look like, but the Syntax I know is not correct:

lnum = len(imgs_urls)
link = urllib2.Request(imgs_urls[0-(lnum)])
output = open('sla' + (0-(lnum)).jpg','wb')
response = urllib2.urlopen(link)
output.write(response.read())
output.close()

It would then save all images, and the file would look something like this:

sla1.png, sla2.png, sla3.png, sla4.png

Any ideas? I think a loop would probably fix this but I don't know how to increment saving the sla.jpg the amount of times of the integer in lnum, and then increment the list number in output the same way.

BilliAm
  • 590
  • 2
  • 6
  • 26

1 Answers1

0

I like to use Python's enumerate to get the index of the iterable in addition to the value. You can use this to auto-increment the value you give to the outputted filenames. Something like this should work:

import urllib2

img_urls = ['http://site/2C2302.png','http://site/2C22101.png','http://site/2C2234.png']

for index, url in enumerate(img_urls):
    link = urllib2.urlopen(url)
    try:
        name = "sla%s.jpg" % (index+1)
        with open(name, "wb") as output:
            output.write(link.read())
    except IOError:
        print "Unable to create %s" % name

You may need to catch other exceptions too, such as permission errors, but that should get you started. Note that I incremented the index by 1 as it is zero-based.

See also:

Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Downloading doesnt seem to be a issue. as I use a Authentication prior to this tied to the `urllib2.urlopen`. For opening the image urls. But those two articles you posted are the original ways of how I found out how to do it in the first place. I will try what you have put above! Thanks! – BilliAm Jul 23 '14 at 16:50
  • You sir are a genius @mike-driscoll. Only one problem came up, and that was missing a s in the list`img(s)_urls`. Other then that it worked EXACTLY how wanted. Saves all the images! One page saved 27. I tried a different page and it saved 4. Awesome. – BilliAm Jul 23 '14 at 17:21