Python Glossary Русский

Python 3.0+urllib.requestterm

urllib

module

A package for working with URLs: parsing and building addresses, encoding parameters, opening URLs over HTTP.

Syntax

from urllib.parse import urlparse

The package has four submodules: urllib.parse parses and builds addresses, urllib.request opens them, urllib.error holds HTTPError and URLError, urllib.robotparser reads robots.txt. In Python 2 this was spread across the urllib, urllib2 and urlparse modules and was merged into this package in Python 3. For parsing addresses urllib.parse is the right choice; for serious HTTP work people usually take the third-party requests, and a bare import urllib does not load the submodules.

Examples

from urllib.parse import urlsplit, urlencode
parts = urlsplit("https://example.com/search?q=python#top")
print(parts.netloc, parts.path, parts.query, parts.fragment)  # → example.com /search q=python top
print(urlencode({"q": "кот", "page": 2}))  # → q=%D0%BA%D0%BE%D1%82&page=2

See also

Open in the full glossarydocs.python.orgReport an inaccuracy