Search code examples
pythonurlurlparse

How do I get urljoin to work as expected in Python?


Let's say I have the following URLs:

url = https://www.example.com/thing1/thing2/thing3
next_thing = thing4

and I want the following URL:

https://www.example.com/thing1/thing2/thing3/thing4

When I try

>>> urlparse.urljoin(url,next_thing) 

I get the following result:

https://www.example.com/thing1/thing2/thing4

Why is thing3 being cut out? And how do I resolve that? Thanks so much!


Solution

  • Trailing slash is missing in the URL, append it to make thing3 a "directory":

    >>> from urlparse import urljoin
    >>> url = "https://www.example.com/thing1/thing2/thing3"
    
    >>> urljoin(url, "thing4")
    'https://www.example.com/thing1/thing2/thing4'
    >>> urljoin(url + "/", "thing4")
    'https://www.example.com/thing1/thing2/thing3/thing4'