The Python Oracle

How to join absolute and relative urls?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Meadow

--

Chapters
00:00 Question
00:22 Accepted answer (Score 308)
00:46 Answer 2 (Score 21)
01:21 Answer 3 (Score 12)
02:37 Answer 4 (Score 11)
02:54 Thank you

--

Full question
https://stackoverflow.com/questions/8223...

Accepted answer links:
[urlparse.urljoin]: https://docs.python.org/2/library/urlpar...
[urlparse is renamed to urllib.parse]: https://docs.python.org/2/library/urlpar...
[use it as follow]: https://docs.python.org/3.6/library/urll...

Answer 2 links:
[How to join components of a path when you are constructing a URL in Python]: https://stackoverflow.com/questions/1793...

Answer 3 links:
[Wikipedia]: https://en.wikipedia.org/wiki/Uniform_Re...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #url

#avk47



ACCEPTED ANSWER

Score 319


You should use urlparse.urljoin :

>>> import urlparse
>>> urlparse.urljoin(url1, url2)
'http://127.0.0.1/test1/test4/test6.xml'

With Python 3 (where urlparse is renamed to urllib.parse) you could use it as follow:

>>> import urllib.parse
>>> urllib.parse.urljoin(url1, url2)
'http://127.0.0.1/test1/test4/test6.xml'



ANSWER 2

Score 23


If your relative path consists of multiple parts, you have to join them separately, since urljoin would replace the relative path, not join it. The easiest way to do that is to use posixpath.

>>> import urllib.parse
>>> import posixpath
>>> url1 = "http://127.0.0.1"
>>> url2 = "test1"
>>> url3 = "test2"
>>> url4 = "test3"
>>> url5 = "test5.xml"
>>> url_path = posixpath.join(url2, url3, url4, url5)
>>> urllib.parse.urljoin(url1, url_path)
'http://127.0.0.1/test1/test2/test3/test5.xml'

See also: How to join components of a path when you are constructing a URL in Python




ANSWER 3

Score 16


For python 3.0+ the correct way to join urls is:

from urllib.parse import urljoin
urljoin('https://10.66.0.200/', '/api/org')
# output : 'https://10.66.0.200/api/org'



ANSWER 4

Score 11


es = ['http://127.0.0.1', 'test1', 'test4', 'test6.xml']
base = ''
map(lambda e: urlparse.urljoin(base, e), es)