I am trying to parse an html for all its img tags, download all the images pointed to by src, and then add those files to a zip file. I would prefer to do all this in memory since I can guarantee there won't be that many images.
Assume the images variable is already populated from parsing the html. What I need help with is getting the images into the zipfile.
from zipfile import ZipFile
from StringIO import StringIO
from urllib2 import urlopen
s = StringIO()
zip_file = ZipFile(s, 'w')
try:
for image in images:
internet_image = urlopen(image)
zip_file.writestr('some-image.jpg', internet_image.fp.read())
# it is not obvious why I have to use writestr() instead of write()
finally:
zip_file.close()