Created
August 9, 2019 13:06
-
-
Save abersheeran/7208f1487fdca5b2ef68fc761e18d0a1 to your computer and use it in GitHub Desktop.
parse relative link to absolute url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_link(current_url: str, link: str) -> str: | |
""" parse relative url to absolute url """ | |
# http(s)://example.com/example | |
if link.startswith("http"): | |
return link | |
# //example.com/example | |
if link.startswith("//"): | |
return current_url.split("//")[0] + link | |
# /example | |
if link.startswith("/"): | |
return re.search('https*://.+?/', current_url).group(0)[:-1] + link | |
# ?query=value | |
if link.startswith("?"): | |
return re.search('https*://.+?\?', current_url).group(0)[:-1] + link | |
# #hash | |
if link.startswith("#"): | |
return current_url.split("#")[0] + link | |
# normal-uri | |
return re.search('https*://.+/', current_url).group(0) + link |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment