如何从部件列表中构建URL路径?

时间:2017-10-05 22:47:03

标签: python

这样做的:

from urllib.parse import urljoin
urljoin('https://site/folder', 'page')

返回https://site/page。那么没关系,我可以附加一个/。但是当我的变量已经有/并且我追加了另一个时,我得到了双重条:

urljoin('https://site/folder//', 'page')
>>> 'https://site/folder//page'

在加入网址时,urljoin允许此双栏//会不会错误?

如何加入这样的网址列表:

urljoin('https://site/folder', 'page', 'otherpage' )
> https://site/folder/page/otherpage

urljoin('https://site/folder', 'page', 'otherpage.jsf' )
> https://site/folder/page/otherpage.jsf

urljoin('https://site/folder/' , 'page.htm', )
> https://site/folder/page.htm

urljoin('https://site/folder//', '/page', '///otherpage' )
> https://site/folder/page/otherpage

urljoin('https://site/folder//', '//page/',  '//otherpage.php'  )
> https://site/folder/page/otherpage.php

urljoin('https://site/folder//', 'page', '/otherpage////' )
> https://site/folder/page/otherpage

4 个答案:

答案 0 :(得分:2)

python docs中提到了此行为。

保留尾部斜杠是附加相应路径组件的合理方法。

答案 1 :(得分:2)

// ...是合法的URI路径。

urljoin检查前一个元素是否有尾随/。如果是这样,它会将其保留为分支而不是叶子。

所以:

>>> urljoin('/foo/bar/','page')
'/foo/bar/page'

>>> urljoin('/foo/bar', 'page')
/foo/page

如果你想真正避免额外的/,那么rstrip()并追加:

>>> urljoin('/foo/bar/'.rstrip('/'), 'page')
'/foo/page'

>>> urljoin('/foo/bar///'.rstrip('/') + '/', 'page')
'/foo/bar/page'

您可能想要做的是:

L = ['root', 'part1','/part2/','//part3//']
urljoin([p.rstrip('/') + '/' for p in L])

答案 2 :(得分:2)

我写了这个URL连接函数:

def _clean_urljoin(url):

    if url.startswith( '/' ) or url.startswith( ' ' ):
        url = url[1:]
        url = _clean_urljoin( url )

    if url.endswith( '/' ) or url.endswith( ' ' ):
        url = url[0:-1]
        url = _clean_urljoin( url )

    return url


def clean_urljoin(*urls):
    fixed_urls = []

    for url in urls:
        fixed_urls.append( _clean_urljoin(url) )

    return "/".join( fixed_urls )


print( clean_urljoin( 'https://site/folder'   , 'page'     , 'otherpage'       ) )
print( clean_urljoin( 'https://site/folder'   , 'page'     , 'otherpage.jsf'   ) )
print( clean_urljoin( 'https://site/folder/'  , 'page.htm' ,                   ) )
print( clean_urljoin( 'https://site/folder//' , '/page'    , '///otherpage'    ) )
print( clean_urljoin( 'https://site/folder//' , '//page/'  , '//otherpage.php' ) )
print( clean_urljoin( 'https://site/folder//' , 'page'     , '/otherpage////'  ) )

运行此命令返回:

$ python3 test.py
https://site/folder/page/otherpage
https://site/folder/page/otherpage.jsf
https://site/folder/page.htm
https://site/folder/page/otherpage
https://site/folder/page/otherpage.php
https://site/folder/page/otherpage

答案 3 :(得分:1)

我确定有不同的方法可以做到这一点

from urllib.parse import urljoin
from functools import reduce # python3

def clean_url(url):
    return url.strip('/') + '/'

def joinurllist(urls):
    return reduce(urljoin, map(clean_url, urls))

joinurllist(['https://site/folder//', 'page', '///otherpage/'])