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.
pcv Jan 16 '19 at 13:17 2019-01-16 13:17
source share