【问题标题】:How to save multiple file from URLs in one zip using python?如何使用python将来自URL的多个文件保存在一个zip中?
【发布时间】:2017-08-18 10:45:00
【问题描述】:

我正在使用漂亮的汤从 URL 中删除文件,然后想使用 Python 将这些文件存储在单个 zip 中。下面是我的一个 URL 的代码 sn-p。

fz = zipfile.ZipFile('C:\\Users\\ADMIN\\data\\data.zip', 'w')


response = urllib2.urlopen(url/file_name.txt)
file = open('C:\\Users\\ADMIN\\data\\filename.txt','w')
file.write(response.read())
file.close()

fz.write('C:\\Users\\ADMIN\\data\\filename.txt',compress_type=zipfile.ZIP_DEFLATED) fz.close()

这个 sn-p 对我不起作用,任何人都可以帮我解决这个问题。出现以下错误:

WindowsError: [错误 2] 系统找不到指定的文件: 'C:\Users\ADMIN\data\filename.txt'

但文件存在于该位置。

【问题讨论】:

  • 似乎在这里工作。

标签: python python-2.7 beautifulsoup zipfile


【解决方案1】:

用途:

fz.writestr("file_name", url.read())

根据需要多次。 IE。每个文件一个 writestr()。在新 ZIP 的开头选择 zip 的模式(放气)。

因此,您无需将文件保存到磁盘,然后打包。只需获取 html 的名称和内容并将它们提供给 writestr()。 ZIP 将“/”作为路径分隔符。因此,您使用类似:“/some_dir/some_subdir/index.html”的子目录或“/index.html”将文件放入根目录。

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多