AttributeError: 'module' object has no attribute 'urlretrieve'
As you're using Python 3, there is no urllib
module anymore. It has been split into several modules.
This would be equivalent to urlretrieve
:
import urllib.requestdata = urllib.request.urlretrieve("http://...")
urlretrieve
behaves exactly the same way as it did in Python 2.x, so it'll work just fine.
Basically:
urlretrieve
saves the file to a temporary file and returns a tuple(filename, headers)
urlopen
returns aRequest
object whoseread
method returns a bytestring containing the file contents
A Python 2+3 compatible solution is:
import sysif sys.version_info[0] >= 3: from urllib.request import urlretrieveelse: # Not Python 3 - today, it is most likely to be Python 2 # But note that this might need an update when Python 4 # might be around one day from urllib import urlretrieve# Get file from URL like this:urlretrieve("http://www-scf.usc.edu/~chiso/oldspice/m-b1-hello.mp3")
Suppose you have following lines of code
MyUrl = "www.google.com" #Your url goes hereurllib.urlretrieve(MyUrl)
If you are receiving following error message
AttributeError: module 'urllib' has no attribute 'urlretrieve'
Then you should try following code to fix the issue:
import urllib.requestMyUrl = "www.google.com" #Your url goes hereurllib.request.urlretrieve(MyUrl)