尝试使用urllib.retrieve通过URL下载文件:'module'对象没有属性'retrieve'

Vio*_*ffe 7 python python-3.x

这是我的第一个Python脚本,所以我假设我做错了.但我无法在任何教程或示例中找到线索.以下代码(可以这么说):

import urllib

urllib.retrieve("http://zlib.net/zlib-1.2.8.tar.gz")
Run Code Online (Sandbox Code Playgroud)

抛出错误

AttributeError:'module'对象没有'retrieve'属性

我如何解决它?这是Python 3.3.

Sud*_*pta 11

[问题在评论中得到了解决,因此现在将其添加为答案.]

以下代码有效.(来源:这个回答)

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

请注意import语句.

你需要做import urllib.request而不是import urllib.