Python Glossary Русский

all Python 3until 3.14Removed from the standard libraryterm

urllib.request.URLopener

removed

The old class for opening and downloading URLs. Removed in Python 3.14 — use urlopen() and urlretrieve().

Syntax

urllib.request.URLopener().open(url)  # до 3.14

URLopener came from the Python 2 urllib module: an object with open() and retrieve() methods that handled the http, ftp, file and data schemes itself. It was removed because since Python 3 the same job goes through the handler system of urllib.request, and the old class got neither fixes nor settings. To migrate, replace opener.open(url) with urllib.request.urlopen(url), preferably in a with block; opener.retrieve(url, filename) becomes urllib.request.urlretrieve(url, filename); headers and proxies are configured through build_opener().

Examples

# Python 3.14+
import urllib.request
try:
    urllib.request.URLopener
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.14+
from pathlib import Path
from urllib.request import urlopen, urlretrieve
Path('note.txt').write_text('hello')
url = Path('note.txt').resolve().as_uri()
with urlopen(url) as resp:
    print(resp.read())  # → b'hello'
path, headers = urlretrieve(url, 'copy.txt')
print(Path(path).read_text())  # → hello
from pathlib import Path
from urllib.request import URLopener
Path('note.txt').write_text('hello')
url = Path('note.txt').resolve().as_uri()
opener = URLopener()
print(opener.open(url).read())  # → b'hello'
path, headers = opener.retrieve(url, 'copy.txt')
print(Path(path).read_text())  # → hello

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy