Python Glossary Русский

all Python 3urllib.requestfunction

urllib.request.urlopen()

opening URLs

Opens a URL (http, https, ftp, file, data) or a Request object and returns a file-like response that is read as bytes.

Syntax

urllib.request.urlopen(url, data=None, timeout=..., *, context=None)

read() returns bytes, not str: decode the text yourself, and resp.headers.get_content_charset() tells you the encoding. Without timeout a request to a hung server waits forever, so pass timeout explicitly. A 4xx/5xx response is not returned but raised as urllib.error.HTTPError (which also has .code and .read()), and a string without a scheme such as 'example.com' raises ValueError. Close the response with a with block.

Examples

import pathlib, urllib.request
path = pathlib.Path('hello.txt')
path.write_text('Привет', encoding='utf-8')
with urllib.request.urlopen(path.resolve().as_uri()) as resp:
    data = resp.read()
print(type(data).__name__, data.decode('utf-8'))  # → bytes Привет
import urllib.request
with urllib.request.urlopen('data:text/plain;charset=utf-8,Hello%20World') as resp:
    print(resp.read(), resp.headers.get_content_charset())  # → b'Hello World' utf-8
urllib.request.urlopen('example.com')  # → ValueError: unknown url type: 'example.com'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy