python3 module ‘urllib’ has no attribute ‘urlretrieve’

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")

 

相關文章

寫留言

你的電子郵件位址並不會被公開。 必要欄位標記為 *