mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
Remove to-be-deprecated urllib.request.urlretrieve function reference (#6454)
This commit is contained in:
parent
b8e21f1289
commit
c89b221758
1 changed files with 12 additions and 4 deletions
|
@ -56,12 +56,20 @@ The simplest way to use urllib.request is as follows::
|
||||||
with urllib.request.urlopen('http://python.org/') as response:
|
with urllib.request.urlopen('http://python.org/') as response:
|
||||||
html = response.read()
|
html = response.read()
|
||||||
|
|
||||||
If you wish to retrieve a resource via URL and store it in a temporary location,
|
If you wish to retrieve a resource via URL and store it in a temporary
|
||||||
you can do so via the :func:`~urllib.request.urlretrieve` function::
|
location, you can do so via the :func:`shutil.copyfileobj` and
|
||||||
|
:func:`tempfile.NamedTemporaryFile` functions::
|
||||||
|
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
import urllib.request
|
import urllib.request
|
||||||
local_filename, headers = urllib.request.urlretrieve('http://python.org/')
|
|
||||||
html = open(local_filename)
|
with urllib.request.urlopen('http://python.org/') as response:
|
||||||
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||||
|
shutil.copyfileobj(response, tmp_file)
|
||||||
|
|
||||||
|
with open(tmp_file.name) as html:
|
||||||
|
pass
|
||||||
|
|
||||||
Many uses of urllib will be that simple (note that instead of an 'http:' URL we
|
Many uses of urllib will be that simple (note that instead of an 'http:' URL we
|
||||||
could have used a URL starting with 'ftp:', 'file:', etc.). However, it's the
|
could have used a URL starting with 'ftp:', 'file:', etc.). However, it's the
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue