How to join absolute and relative urls?

I have two URLs:

url1 = "http://127.0.0.1/test1/test2/test3/test5.xml" url2 = "../../test4/test6.xml" 

How can I get the absolute url for url2?

+77
python url
Nov 22 '11 at 8:32
source share
4 answers

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 urllib.parse ) you can use it like this:

 >>> import urllib.parse >>> urllib.parse.urljoin(url1, url2) 'http://127.0.0.1/test1/test4/test6.xml' 
+154
Nov 22 '11 at 8:35
source share
 es = ['http://127.0.0.1', 'test1', 'test4', 'test6.xml'] base = '' map(lambda e: urlparse.urljoin(base, e), es) 
+6
Jan 21 '14 at 17:08
source share
 >>> from urlparse import urljoin >>> url1 = "http://www.youtube.com/user/khanacademy" >>> url2 = "/user/khanacademy" >>> urljoin(url1, url2) 'http://www.youtube.com/user/khanacademy' 

Simple

+4
Jan 15 '14 at 15:40
source share

If your relative path consists of several parts, you must join them separately, as urljoin will replace the relative path, not attach it. The easiest way to do this 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 combine path components when creating a URL in Python.

0
Jan 16 '19 at 13:17
source share



All Articles