Is there predefined class for URL in Python?

What we have as of 2018:

  • purl,
  • YURL,
  • furl.

Only furl is being maintained today but its major disadvantage is that it's mutable, that doesn't encourage best practices, of course. (There is good modern reference — pathlib which consists of immutable classes.)

Overall, having a painless OO way to parse and construct URLs is graeat.

Update

yarl is worth looking at.


urlparse does encapsulate URLs into a class, called ParseResult, so it can be considered a factory function for these. Straight from the Python docs:

>>> urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')

If you desperately want a class called URL to encapsulate your URLs, use an alias (URL = urlparse.ParseResult) or create an adapter.


You might want consider having a look at furl because it might be an answer to your needs.


~10 years late to the party here, but today, pydantic provides several URL types that might be helpful for validating, storing and passing around URLs; with type hints and mypy becoming more and more prevalent nowadays, some might consider this some kind of standard.