2

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!

codycrossley
  • 571
  • 1
  • 6
  • 17

2 Answers2

3

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'
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Interesting! Thank you @alecxe! I will try this and like/accept your answer if it works. In the mean time, do you know why the trailing slash makes a difference? – codycrossley Jul 28 '15 at 16:51
  • @codycrossley without a slash `thing3` is a considered "filename", some explanation is available here: http://stackoverflow.com/a/4317446/771848. Glad it helped. – alecxe Jul 28 '15 at 17:12
  • ahhh, makes sense! Thanks a ton! – codycrossley Jul 28 '15 at 17:15
-1

Not sure but think this might be what you're looking for.

url = 'https://www.example.com/thing1/thing2/thing3'
next_thing = 'thing4'
test = url + next_thing
print(test)

Comes out with:

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