Python:与 urljoin 混淆

标签 python python-3.x

我正在尝试从不同的部分形成 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/

相关文章:

python-3.x - 我试图检查索引是否超出范围,但是我使用的代码给出的索引超出范围?

javascript - 无法在两个 Python aiortc 脚本之间完全建立 ICE 连接

python - 在 Python 中创建用户配置文件

python - scipy interp1d 中的错误

python - 当 Python 中的 py_compile 输入路径不同时,abspath 返回不同的结果?

python - 确定 TreeView 行的高度

python - 在python中将unicode转换为两个单独的列表

用于 Python 3 的 wxPython

python - 通过用 0 替换元素生成所有可能的列表

python - 在 Python 中使用 Pandas 进行特征工程,每次计算使用多行