Python:与 urljoin 的混淆 [英] Python: confusions with urljoin

查看:50
本文介绍了Python:与 urljoin 的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从不同的部分形成 URL,但无法理解此方法的行为.例如:

Python 3.x

from urllib.parse import urljoin>>>urljoin('一些','东西')'事物'>>>urljoin('http://some', '东西')'http://some/thing'>>>urljoin('http://some/more', '东西')'http://some/thing'>>>urljoin('http://some/more/', 'thing') # 只是一点点/在 'more' 之后'http://some/more/thing'urljoin('http://some/more/', '/thing')'http://some/thing'

你能解释一下这个方法的确切行为吗?

解决方案

(对我而言)最好的方式是第一个参数,base 就像你在浏览器.第二个参数 url 是该页面上的锚点的 href.结果是您点击后将被定向到的最终网址.

<预><代码>>>>urljoin('一些','东西')'事物'

根据我的描述,这个是有道理的.尽管人们希望 base 包含一个方案和域.

<预><代码>>>>urljoin('http://some', '东西')'http://some/thing'

如果您在某个虚拟主机上,并且有一个像 <a href='thing'>Foo</a> 这样的锚点,那么该链接会将您带到 http://some/thing

<预><代码>>>>urljoin('http://some/more', '东西')'http://some/thing'

我们在 some/more 此处,因此 thing 的相对链接将带我们到 /some/thing

<预><代码>>>>urljoin('http://some/more/', 'thing') # 只是一点点/在 '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.

I am trying to form URLs from different pieces, and having trouble understanding the behavior of this method. For example:

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'

Can you explain the exact behavior of this method?

解决方案

The best way (for me) to think of this is the first argument, base is like the page you are on in your browser. The second argument url is the href of an anchor on that page. The result is the final url to which you will be directed should you click.

>>> urljoin('some', 'thing')
'thing'

This one makes sense given my description. Though one would hope base includes a scheme and domain.

>>> urljoin('http://some', 'thing')
'http://some/thing'

If you are on a vhost some, and there is an anchor like <a href='thing'>Foo</a> then the link will take you to http://some/thing

>>> urljoin('http://some/more', 'thing')
'http://some/thing'

We are on some/more here, so a relative link of thing will take us to /some/thing

>>> urljoin('http://some/more/', 'thing') # just a tad / after 'more'
'http://some/more/thing'

Here, we aren't on some/more, we are on some/more/ which is different. Now, our relative link will take us to some/more/thing

>>> urljoin('http://some/more/', '/thing')
'http://some/thing'

And lastly. If on some/more/ and the href is to /thing, you will be linked to some/thing.

这篇关于Python:与 urljoin 的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆