python - 下载一个 zip 文件并使用 Python3 将其解压缩到内存中

标签 python python-3.x python-3.3 python-requests

我想从互联网上下载一个 zip 文件并解压。

我宁愿使用请求。我不想写入磁盘。

我知道如何在 Python2 中做到这一点,但我对 python3.3 毫 headless 绪。显然,zipfile.Zipfile 想要一个类似文件的对象,但我不知道如何从请求返回的内容中获取它。

如果您知道如何使用 urllib.request 做到这一点,我也很想知道您是如何做到的。

最佳答案

我知道怎么做了:

request = requests.get(url)
file = zipfile.ZipFile(BytesIO(request.content))

我错过了什么:

  • request.content 应该用来访问字节
  • io.BytesIObytes 的正确类文件对象。

关于python - 下载一个 zip 文件并使用 Python3 将其解压缩到内存中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23419322/

相关文章:

python - 如何使用 Python Jenkins API 在 Jenkins 中查找和查询特定构建

python - 这个简单的If语句向我发送了SyntaxError : invalid syntax on Python 3.6

python - wxpython/python中的 '&string'是什么意思

python - 如何在 pygame 中获取两个碰撞的 Sprite 对象?

Python 异步/等待执行时间的差异

python - Django Rest Framework 中的批量更新数据

python - Python Descriptors的具体实现

python-3.x - 为什么本地协程的 yield 语法是有效的

python - 为什么在 Python 中从不同路径调用类时 __class__ 不同?

Python 未关闭资源 : is it safe to delete the file?