Python:与urljoin的混淆

Python:与urljoin的混淆,python,python-3.x,Python,Python 3.x,我试图从不同的部分形成URL,但在理解这种方法的行为时遇到了困难。例如: Python3.x from urllib.parse import urljoin >>> urljoin('some', 'thing') 'thing' >>> urljoin('http://some', 'thing') 'http://some/thing' >>> urljoin('http://some/more', 'thing') 'http://

我试图从不同的部分形成URL,但在理解这种方法的行为时遇到了困难。例如:

Python3.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
是该页面上锚的href。结果是最终的url,如果单击,您将被定向到该url

>>> urljoin('some', 'thing')
'thing'
根据我的描述,这一点是有道理的。尽管人们希望基础包括一个方案和域

>>> urljoin('http://some', 'thing')
'http://some/thing'
如果您在vhost-some上,并且有一个类似
的锚,那么链接将带您到
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'
我们在这里看到的是
some/more
,因此
thing
的相对链接将把我们带到
/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'
在这里,我们不是在
some/more
,而是在
some/more/
上,这是不同的。现在,我们的相对链接将把我们带到
some/more/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'
最后。如果打开
some/more/
,href是to
/thing
,您将被链接到
some/thing

(对我来说)思考这一点的最佳方式是第一个参数,
base
就像您在浏览器中的页面一样。第二个参数
url
是该页面上锚的href。结果是最终的url,如果单击,您将被定向到该url

>>> urljoin('some', 'thing')
'thing'
根据我的描述,这一点是有道理的。尽管人们希望基础包括一个方案和域

>>> urljoin('http://some', 'thing')
'http://some/thing'
如果您在vhost-some上,并且有一个类似
的锚,那么链接将带您到
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'
我们在这里看到的是
some/more
,因此
thing
的相对链接将把我们带到
/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'
在这里,我们不是在
some/more
,而是在
some/more/
上,这是不同的。现在,我们的相对链接将把我们带到
some/more/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'
最后。如果打开
some/more/
,且href指向
/thing
,您将链接到
some/thing

urllib.parse.urljoin(基本,url

如果url是绝对url(即,以//、http:/、https:/、…)开头),则url的主机名和/或方案将出现在 结果。例如:

否则,urllib.parse.urljoin(base,url)将

通过将“基本URL”(基本URL)与另一个URL(URL)组合,构建完整(“绝对”)URL。非正式地说,它使用基础的组件 URL,尤其是寻址方案、网络位置和 (部分)路径,以提供相对URL中缺少的组件

它获取第一个参数(base)的路径,剥离最后一个/之后的部分,并与第二个参数(url)连接

如果url以/开头,它将使用url连接基的scheme和netloc

>>>urljoin('http://a/b/c/d/e', '/f')
'http://a/f'
urllib.parse.urljoin(基本,url

如果url是绝对url(即,以//、http:/、https:/、…)开头),则url的主机名和/或方案将出现在 结果。例如:

否则,urllib.parse.urljoin(base,url)将

通过将“基本URL”(基本URL)与另一个URL(URL)组合,构建完整(“绝对”)URL。非正式地说,它使用基础的组件 URL,尤其是寻址方案、网络位置和 (部分)路径,以提供相对URL中缺少的组件

它获取第一个参数(base)的路径,剥离最后一个/之后的部分,并与第二个参数(url)连接

如果url以/开头,它将使用url连接基的scheme和netloc

>>>urljoin('http://a/b/c/d/e', '/f')
'http://a/f'

请注意:上面的import语句是针对Python3.x的。对Python2.x使用“from urlparse import urljoin”。注意:上面的import语句是针对Python3.x的。在Python2.x中使用“from urlparse import urljoin”。感谢您的解释。。。这种行为使look for'true'
urljoin
,其行为类似于
os.path.join
对于那些只想在另一个url上添加一位url的人来说,没有urljoin的逻辑,posixpath.join()可能适合你。我喜欢
urljoin('http://','some/','thing')
它的结局是:
http:///some/“
”\_(ツ)_/’感谢您的解释……这种行为使look for‘true’
urljoin
,其行为类似于
os.path.join
对于那些只想在另一个url上添加一位url的人来说,没有urljoin的逻辑,posixpath.join()可能适合您。我喜欢
urljoin('http://','some/','thing'))
它的结局是:
'http:///some/“
”\_(ツ)_/¯