This is my first ever Python script, so I assume I'm doing something wrong. But I can't find a clue in any of the tutorials or examples. The following code (so to speak):
import urllib
urllib.retrieve("http://zlib.net/zlib-1.2.8.tar.gz")
throws an error
AttributeError: 'module' object has no attribute 'retrieve'
How do I fix it? This is Python 3.3.
urllib is a package that collects several modules for working with URLs: urllib. request for opening and reading URLs. urllib. error containing the exceptions raised by urllib.
[The question was solved in the comments, hence adding it as an answer now.]
The below code works. (Source : this answer)
import urllib.request
# Download the file from `url` and save it locally under `file_name`:
urllib.request.urlretrieve(url, file_name)
Note the import statement.
You need to do import urllib.request
instead of import urllib
.
As the error says, urllib
does not have a retrieve
function.
In Python 2, the module did have a urlretrieve
function. In Python 3, that function has been moved to urllib.request.urlretrieve
.
You can find all this in the documentation: https://docs.python.org/3/library/urllib.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With