img html可否链接zip中的图像,解析html文件并将找到的图像添加到zip fi

为了回答您关于如何创建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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值