Comments¶
It is suggested that you use the requests module instead of urllib unless you want to have minimal 3rd-party dependencies.
You have to explicit import
urllib.request
in order to use it in Python 3. Please refer to https://bugs.python.org/issue36701 for more discussions. This is how Python 3 intends to work generally speaking. Of course, there are a few exceptions such asos.path
.
import urllib.request
urllib.request.urlopen¶
r = urllib.request.urlopen("https://github.com/dclong/dsutil/releases/latest")
r.url
urllib.request.urlretrieve¶
urllib.request.urlretrieve
can be used to download a file from the internet to local.
file, obj = urllib.request.urlretrieve(
"http://www.legendu.net/media/download_code_server.py",
"/tmp/download_code_server.py",
)
file
obj
!ls /tmp/download_code_server.py
type(obj)
dir(obj)
obj.as_string()
References¶
https://stackabuse.com/download-files-with-python/
https://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python
https://docs.python.org/3/library/http.client.html#http.client.HTTPResponse
https://docs.python.org/3/library/urllib.request.html#module-urllib.request