这是我的第一个 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/