使用Python将相对URL转换为完全限定的URL

Mik*_*sen 6 python string url http

我正在寻找一种使用Python完全限定URL的方法.我有我当前的页面URL,例如:

http://www.foo.com/Stuff/Mike/Doc.html

我有我的href,例如:

HREF = "../鲍勃/ Doc.html"

我需要建立的是:

http://www.foo.com/Stuff/Bob/Doc.html

Python有没有可以解析这样的路径的库?我查看了urllib和urllib2的文档,但找不到那样的东西.谢谢!

che*_*ken 9

使用该urlparse库.

>>> import urlparse
>>> urlparse.urljoin("http://www.foo.com/Stuff/Mike/Doc.html","../Bob/Doc.html")
'http://www.foo.com/Stuff/Bob/Doc.html'
Run Code Online (Sandbox Code Playgroud)