[英]How to save multiple file from URLs in one zip using python?
I am scrapping the files from URLs using beautiful soup, and then want to store those files in a single zip using Python. 我正在使用漂亮的汤从URL抓取文件,然后希望使用Python将这些文件存储在单个zip中。 Below is my code snippet for one URL.
以下是我针对一个网址的代码段。
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()
This snippet is not working for me can any one please help me on this. 此代码段对我不起作用,任何人都可以帮助我。 getting below error:
低于错误:
WindowsError: [Error 2] The system cannot find the file specified: 'C:\\Users\\ADMIN\\data\\filename.txt'
WindowsError:[错误2]系统找不到指定的文件:'C:\\ Users \\ ADMIN \\ data \\ filename.txt'
but file is present in this location. 但是该位置存在文件。
Use: 采用:
fz.writestr("file_name", url.read())
as many times as you need. 根据需要多次。 Ie one writestr() per file.
即每个文件一个writestr()。 Select the zip's mode (deflated) at the opening of the new ZIP.
在新ZIP的开头选择zip的模式(放气)。
So, you do not need to save file to disk, then pack it. 因此,您无需将文件保存到磁盘,然后打包。 Just get the html's name and the content and feed them to writestr().
只需获取html的名称和内容并将其提供给writestr()。 ZIP gets '/' as a path separator.
ZIP将'/'作为路径分隔符。 Therefore you use something like: "/some_dir/some_subdir/index.html" for subdirectories or "/index.html" to put a file into root.
因此,您可以使用诸如:“ / some_dir / some_subdir / index.html”作为子目录,或使用“ /index.html”将文件放入根目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.