假设我有以下网址:
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 中缺少尾部斜杠,请将其附加以使 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'
关于python - 如何让 urljoin 在 Python 中按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31682359/