为了回答您关于如何创建ZIP归档的具体问题(这里的其他人已经讨论过解析url),我测试了您的代码。你真的离成品已经很近了。在
下面是我如何增加创建Zip存档所需的内容(在本例中,我正在将存档写入驱动器,以便验证它是否正确写入)。在from zipfile import ZipFile, ZipInfo, ZIP_DEFLATED
import zlib
from cStringIO import StringIO
from urllib2 import urlopen
from urlparse import urlparse
from os import path
images = ['http://sstatic.net/so/img/logo.png',
'http://sstatic.net/so/Img/footer-cc-wiki-peak-internet.png']
buf = StringIO()
# By default, zip archives are not compressed... adding ZIP_DEFLATED
# to achieve that. If you don't want that, or don't have zlib on or
# system, delete the compression kwarg
zip_file = ZipFile(buf, mode='w', compression=ZIP_DEFLATED)
for image in images:
internet_image = urlopen(image)
fname = path.basename(urlparse(image).path)
zip_file.writestr(fname, internet_image.read())
zip_file.close()
output = open('images.zip', 'wb')
output.write(buf.getvalue())
output.close()
buf.close()