1

Currently my python script (which uses 'Spynner') has a loop which contains:

browser.load(url)
time.sleep(5)
browser.snapshot().save('example.png')

...but the problem is that every time the loop cycles it overwrites the previously stored image with an image of the newly rendered URL (the URL changes each time the loop is run) instead of making a new image, separate from the previous one. I have created a count but for some reason:

browser.snapshot().save((count)'.png')

doesn't seem to work. How do you use a defined count as part of a filename with .save?

Thanks

Studiumcirclus
  • 69
  • 4
  • 11

2 Answers2

1
browser.snapshot().save('{:03d}.png'.format(count))

should do it. You can change the format operator (:03d) to a bigger number if you need to.

This will give you files with the format 000.png, 001.png, etc. Just using str(count)+'.png' will not pad the zeroes at the front, making for more difficult file handling later

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Thanks for that! If I needed support for up to 10,000 file-names (instead of up to 999, within the hundreds) would I use "{:0005d}" instead of "{:03d}"? I assumed the number of zeros and the "[number]d" were to do with the number of digits required? – Studiumcirclus Jun 05 '15 at 11:25
  • 1
    No, just `{:05d}` will do it. The 0 means 'pad zeros', the 5d means a 5 digit integer – tmdavison Jun 05 '15 at 11:25
  • Thanks! I almost had it haha :) much appreciated. – Studiumcirclus Jun 05 '15 at 11:30
0

try this

for counter,url in enumerate(urls):
    browser.load(url)
    time.sleep(5)
    browser.snapshot().save(str(counter) + '.png')

or just set counter = 0, then run the loop, incrementing counter after each save like this : -

counter = 0
for url in urls:
    browser.load(url)
    time.sleep(5)
    browser.snapshot().save(str(counter) + '.png')
    counter += 1
Namit Singal
  • 1,506
  • 12
  • 26