You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from purl import URL
from urllib import parse
def resolve(base, url):
"""
Resolves a target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF
:param base: str|URL
:param url: str|URL
:return: URL
"""
if isinstance(base, URL):
baseurl = base
else:
baseurl = URL(base)
if isinstance(url, URL):
relurl = url
else:
relurl = URL(url)
if relurl.host():
return relurl
if relurl.path():
return URL(
scheme=baseurl.scheme(),
host=baseurl.host(),
port=baseurl.port(),
path=parse.urljoin(baseurl.path(), relurl.path()),
query=relurl.query(),
fragment=relurl.fragment(),
)
elif relurl.query() or '?' in url:
return URL(
scheme=baseurl.scheme(),
host=baseurl.host(),
port=baseurl.port(),
path=baseurl.path(),
query=relurl.query(),
fragment=relurl.fragment(),
)
elif relurl.fragment() or '#' in url:
return URL(
scheme=baseurl.scheme(),
host=baseurl.host(),
port=baseurl.port(),
path=baseurl.path(),
query=baseurl.query(),
fragment=relurl.fragment(),
)
return baseurl
Am not sure about this one. I can see how the ../home request translates to a path change but values like ? already have an API in purl (setting the query string to an empty string).
Have you seen an API like this in another URL lib?
resolve() is more than just urllib.parse.urljoin(). It resolves a target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF.
Here's the helper function I wrote:
Usage:
The text was updated successfully, but these errors were encountered: