Back to main.

Calmcode Shorts

URLs adhere to a standard.


The benefit of such a standard is that we might parse out the parts that we're interested in. Before doing that yourself though, it'd be better to check yarl. Yarl stands for Yet Another URL Library and it makes it easy to deal with URLs.

Here's an example of how you might use the library.

from yarl import URL

url = URL(''), url.scheme, url.path, url.query_string, url.query, url.fragment

The library can also deal with url encodings that are percent-decoded. A common example of this in a URL would be the whitespace character.

url = URL(' world')
url.path, url.raw_path, url.human_repr()

Finally, it's also good to know that yarl doesn't just parse URLs. It can also be used to build them. There's the .build() constructor."https", host="", query={"a": "b", "number": 1})

But you can also create a URL by chaining together appropriate python operators. It's similar to the syntax in pathlib.

URL("") / 'things' % {"prop1": 1, "prop2": "a"}

Back to main.