如何加入绝对和相对网址?

时间:2011-11-22 08:33:00

标签: python url

我有两个网址:

url1 = "http://127.0.0.1/test1/test2/test3/test5.xml"
url2 = "../../test4/test6.xml"

如何获取url2的绝对网址?

5 个答案:

答案 0 :(得分:170)

您应该使用urlparse.urljoin

>>> import urlparse
>>> urlparse.urljoin(url1, url2)
'http://127.0.0.1/test1/test4/test6.xml'

使用Python 3(urlparse is renamed to urllib.parse)你可以use it as follow

>>> import urllib.parse
>>> urllib.parse.urljoin(url1, url2)
'http://127.0.0.1/test1/test4/test6.xml'

答案 1 :(得分:6)

es = ['http://127.0.0.1', 'test1', 'test4', 'test6.xml']
base = ''
map(lambda e: urlparse.urljoin(base, e), es)

答案 2 :(得分:4)

>>> from urlparse import urljoin
>>> url1 = "http://www.youtube.com/user/khanacademy"
>>> url2 = "/user/khanacademy"
>>> urljoin(url1, url2)
'http://www.youtube.com/user/khanacademy'

简单。

答案 3 :(得分:2)

如果您的相对路径由多个部分组成,则必须分别将它们连接起来,因为urljoin会替换相对路径而不是将其连接起来。最简单的方法是使用posixpath

>>> import urllib.parse
>>> import posixpath
>>> url1 = "http://127.0.0.1"
>>> url2 = "test1"
>>> url3 = "test2"
>>> url4 = "test3"
>>> url5 = "test5.xml"
>>> url_path = posixpath.join(url2, url3, url4, url5)
>>> urllib.parse.urljoin(url1, url_path)
'http://127.0.0.1/test1/test2/test3/test5.xml'

另请参阅:How to join components of a path when you are constructing a URL in Python

答案 4 :(得分:0)

对于python 3.0+,加入url的正确方法是:

from urllib.parse import urljoin
urljoin('https://10.66.0.200/', '/api/org')
# output : 'https://10.66.0.200/api/org'