如何让 urljoin 在 Python 中按预期工作?
How do I get urljoin to work as expected in Python?
假设我有以下 URLs:
url = https://www.example.com/thing1/thing2/thing3
next_thing = thing4
我想要以下 URL:
https://www.example.com/thing1/thing2/thing3/thing4
当我尝试时
>>> urlparse.urljoin(url,next_thing)
我得到以下结果:
https://www.example.com/thing1/thing2/thing4
为什么 thing3
被删掉了?我该如何解决?非常感谢!
不确定,但我想这可能就是您要找的。
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
URL 中缺少尾部斜线,请将其附加到 thing3
为 "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'
假设我有以下 URLs:
url = https://www.example.com/thing1/thing2/thing3
next_thing = thing4
我想要以下 URL:
https://www.example.com/thing1/thing2/thing3/thing4
当我尝试时
>>> urlparse.urljoin(url,next_thing)
我得到以下结果:
https://www.example.com/thing1/thing2/thing4
为什么 thing3
被删掉了?我该如何解决?非常感谢!
不确定,但我想这可能就是您要找的。
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
URL 中缺少尾部斜线,请将其附加到 thing3
为 "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'