python - 尝试使用 urllib.retrieve : 'module' object has no attribute 'retrieve' 通过 URL 下载文件

标签 python python-3.x

这是我的第一个 Python 脚本,所以我认为我做错了什么。但是我在任何教程或示例中都找不到线索。以下代码(可以这么说):

import urllib

urllib.retrieve("http://zlib.net/zlib-1.2.8.tar.gz")

抛出一个错误

AttributeError: 'module' object has no attribute 'retrieve'

我该如何解决?这是 Python 3.3。

最佳答案

[问题已在评论中解决,因此现在将其添加为答案。]

下面的代码有效。 (来源:this answer)

import urllib.request
# Download the file from `url` and save it locally under `file_name`:
urllib.request.urlretrieve(url, file_name)

注意导入语句。

您需要执行 import urllib.request 而不是 import urllib

关于python - 尝试使用 urllib.retrieve : 'module' object has no attribute 'retrieve' 通过 URL 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31432597/

相关文章:

python - 字符串转字典,key是以 "."结尾的词

python - 如何使用 suds (Python) 对 HTTP 代理进行身份验证?

python - 如何更新 Django 模型数据

python - 在 python 中打印下标

python - 使用 Python 线程对具有速率限制的慢速 API 进行数千次调用

python-3.x - 如何在 python 中的 DXF 中查找形状的尺寸?

python - 选择和部署 cometd 服务器

python - 如何让 AppEngine map reduce 横向扩展?

python - 使用 mypy 时正确输入 python 中的异常/错误元组

python - 使用 urllib 在 python 中下载 pdf 文件会导致一些 html 垃圾