我正在尝试从不同的部分形成 URL,但无法理解此方法的行为。例如:
Python 3.x
from urllib.parse import urljoin
>>> urljoin('some', 'thing')
'thing'
>>> urljoin('http://some', 'thing')
'http://some/thing'
>>> urljoin('http://some/more', 'thing')
'http://some/thing'
>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'
urljoin('http://some/more/', '/thing')
'http://some/thing'
你能解释一下这个方法的确切行为吗?
最佳答案
(对我来说)最好的方法是第一个参数,base
就像您在浏览器中所在的页面一样。第二个参数url
是该页面上 anchor 的 href。结果是您点击后将被定向到的最终 url。
>>> urljoin('some', 'thing')
'thing'
根据我的描述,这个是有道理的。虽然有人希望 base 包含一个方案和域。
>>> urljoin('http://some', 'thing')
'http://some/thing'
如果你在一个虚拟主机上,并且有一个像 <a href='thing'>Foo</a>
这样的 anchor 然后链接将带您到http://some/thing
>>> urljoin('http://some/more', 'thing')
'http://some/thing'
我们在 some/more
这里是thing
的相对链接将带我们到/some/thing
>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'
这里,我们不在 some/more
, 我们在 some/more/
这是不同的。现在,我们的相对链接将带我们到 some/more/thing
>>> urljoin('http://some/more/', '/thing')
'http://some/thing'
最后。如果在 some/more/
并且href是/thing
,您将链接到 some/thing
.
关于Python:与 urljoin 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10893374/