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

时间:2015-07-28 16:40:23

标签: python url urlparse

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!

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

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