usuario una script ruta relativa que entre diferenciar cómo absoluta python url path

python - script - Resolución de una ruta de URL relativa a su ruta absoluta



ruta relativa script (1)

¿Hay una biblioteca en python que funcione así?

>>> resolvePath("http://www.asite.com/folder/currentpage.html", "anotherpage.html") ''http://www.asite.com/folder/anotherpage.html'' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") ''http://www.asite.com/folder/folder2/anotherpage.html'' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") ''http://www.asite.com/folder3/anotherpage.html'' >>> resolvePath("http://www.asite.com/folder/currentpage.html", "../finalpage.html") ''http://www.asite.com/finalpage.html''


Sí, existe urlparse.urljoin o urllib.parse.urljoin para Python 3.

>>> try: from urlparse import urljoin # Python2 ... except ImportError: from urllib.parse import urljoin # Python3 ... >>> urljoin("http://www.asite.com/folder/currentpage.html", "anotherpage.html") ''http://www.asite.com/folder/anotherpage.html'' >>> urljoin("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") ''http://www.asite.com/folder/folder2/anotherpage.html'' >>> urljoin("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") ''http://www.asite.com/folder3/anotherpage.html'' >>> urljoin("http://www.asite.com/folder/currentpage.html", "../finalpage.html") ''http://www.asite.com/finalpage.html''

para copiar y pegar:

try: from urlparse import urljoin # Python2 except ImportError: from urllib.parse import urljoin # Python3