python3 module ‘urllib’ has no attribute ‘urlretrieve’

Posted in :

urlretrieve 在 python3 裡寫法不一樣了。會有 Error:

AttributeError: module 'urllib' has no attribute 'urlretrieve'

 

解法:

import sys

if sys.version_info[0] >= 3:
    from urllib.request import urlretrieve
else:
    # 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://your-url-here")

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *