2

假设我有以下网址:

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

我想要以下网址:

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

当我尝试

>>> urlparse.urljoin(url,next_thing) 

我得到以下结果:

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

为什么会thing3被剪掉?我该如何解决?非常感谢!

4

2 回答 2

3

URL 中缺少尾部斜杠,附加它以创建thing3一个“目录”:

>>> 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'
于 2015-07-28T16:44:40.740 回答
0

不确定,但认为这可能是您正在寻找的。

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

出来:

https://www.example.com/thing1/thing2/thing3/thing4
于 2015-07-28T16:44:05.893 回答