urllib
https://docs.python.org/3/library/urllib.html
Table of Contents
Check if a URL is absolute howto
urlparse
parses a URL asscheme://netloc/path;parameters?query#fragment
- By testing whether or not
netloc
is empty, we can determine that a URL is absolute.
from urllib.parse import urlparse
def is_absolute(url):
return bool(urlparse(url).netloc)
print(is_absolute('https://yeonghoey.com/python'))
print(is_absolute('python/_img'))
True
False
Remove the fragment from a URL howto
- Use
urllib.parse.urldefrag
.
from urllib.parse import urldefrag
tuple_ = urldefrag('https://yeonghoey.com/python/#how-to')
print(tuple_)
DefragResult(url='https://yeonghoey.com/python/', fragment='how-to')