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

标签 python url

我有两个网址:

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

如何获得 url2 的绝对网址?

最佳答案

您应该使用 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'

关于python - 如何加入绝对和相对网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8223939/

相关文章:

php - Laravel - 以 .php 结尾的重定向捕获/重定向 Urls

python - 绘制 Matplotlib 中的缺陷

php - 在没有 PHP 的情况下模拟 PHP 包含

python - 如何给用户组授予访问权限?

python - 解包 n 级嵌套列表

c - 在C glib中解析url查询参数?

.htaccess - ISAPI 重写查询字符串

c++ - InternetCrackUrl 没有正确返回 lpszHostName

python - 如何警告我潜在的 NameError?

python - 无法更新 DataFrame 中的特定条目