urlstd.parse.URL#

class urlstd.parse.URL(url: str, base: str | URL | None = None)

Bases: object

Parses a string url against a base URL base.

Parameters:
  • url – An absolute-URL or a relative-URL. If url is a relative-URL, base is required.

  • base – An absolute-URL for a relative-URL url.

Raises:

urlstd.error.URLParseError – Raised when URL parsing fails.

Examples

To parse a string into a URL:

>>> URL('http://user:pass@foo:21/bar;par?b#c')
<URL(href='http://user:pass@foo:21/bar;par?b#c', origin='http://foo:21',
protocol='http:', username='user', password='pass', host='foo:21',
hostname='foo', port='21', pathname='/bar;par', search='?b', hash='#c')>

To parse a string into a URL with using a base URL:

>>> URL('//foo/bar', base='http://example.org/foo/bar')
<URL(href='http://foo/bar', origin='http://foo', protocol='http:',
username='', password='', host='foo', hostname='foo', port='',
pathname='/bar', search='', hash='')>
>>> URL('/', base='http://example.org/foo/bar')
<URL(href='http://example.org/', origin='http://example.org',
protocol='http:', username='', password='', host='example.org',
hostname='example.org', port='', pathname='/', search='', hash='')>
>>> URL('https://test:@test', base='about:blank')
<URL(href='https://test@test/', origin='https://test',
protocol='https:', username='test', password='', host='test',
hostname='test', port='', pathname='/', search='', hash='')>
>>> URL('?a=b&c=d', base='http://example.org/foo/bar')
<URL(href='http://example.org/foo/bar?a=b&c=d',
origin='http://example.org', protocol='http:', username='', password='',
host='example.org', hostname='example.org', port='',
pathname='/foo/bar', search='?a=b&c=d', hash='')>
>>> URL('#β', base='http://example.org/foo/bar')
<URL(href='http://example.org/foo/bar#%CE%B2',
origin='http://example.org', protocol='http:', username='', password='',
host='example.org', hostname='example.org', port='',
pathname='/foo/bar', search='', hash='#%CE%B2')>
>>> URL('', base='http://example.org/foo/bar')
<URL(href='http://example.org/foo/bar', origin='http://example.org',
protocol='http:', username='', password='', host='example.org',
hostname='example.org', port='', pathname='/foo/bar', search='',
hash='')>
>>> URL('https://x/\ufffd?\ufffd#\ufffd', base='about:blank')
<URL(href='https://x/%EF%BF%BD?%EF%BF%BD#%EF%BF%BD', origin='https://x',
protocol='https:', username='', password='', host='x', hostname='x',
port='', pathname='/%EF%BF%BD', search='?%EF%BF%BD', hash='#%EF%BF%BD')>

Methods:

__eq__(other)

Returns True if other is equal to this object.

__str__()

Returns a string representation of a URL.

can_parse(url[, base])

Returns True if url against a base URL base is parsable.

equals(other[, exclude_fragments])

Returns True if other is equal to this object.

Attributes:

hash

A URL’s fragment (includes leading U+0023 (#) if non-empty).

host

A URL’s host, and then, if a URL’s port is different from the default port for a URL’s scheme, U+003A (:), followed by URL’s port.

hostname

A URL’s host.

href

A string representation of a URL.

origin

Returns a string representation of a URL’s origin.

password

A URL’s password.

pathname

A URL’s path.

port

A URL’s port.

protocol

A URL’s scheme, followed by U+003A (:).

search

A URL’s query (includes leading U+003F (?) if non-empty).

search_params

Returns a URLSearchParams object associated with this URL object.

username

A URL’s username.