2009-01-24 214 views
49

python中是否有像這樣的庫?將相對url路徑解析爲其絕對路徑

>>> resolvePath("http://www.asite.com/folder/currentpage.html", "anotherpage.html") 
'http://www.asite.com/folder/anotherpage.html' 
>>> resolvePath("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") 
'http://www.asite.com/folder/folder2/anotherpage.html' 
>>> resolvePath("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") 
'http://www.asite.com/folder3/anotherpage.html' 
>>> resolvePath("http://www.asite.com/folder/currentpage.html", "../finalpage.html") 
'http://www.asite.com/finalpage.html' 

回答

85

是的,有urlparse.urljoin,或urllib.parse.urljoin爲Python 3

>>> try: from urlparse import urljoin # Python2 
... except ImportError: from urllib.parse import urljoin # Python3 
... 
>>> urljoin("http://www.asite.com/folder/currentpage.html", "anotherpage.html") 
'http://www.asite.com/folder/anotherpage.html' 
>>> urljoin("http://www.asite.com/folder/currentpage.html", "folder2/anotherpage.html") 
'http://www.asite.com/folder/folder2/anotherpage.html' 
>>> urljoin("http://www.asite.com/folder/currentpage.html", "/folder3/anotherpage.html") 
'http://www.asite.com/folder3/anotherpage.html' 
>>> urljoin("http://www.asite.com/folder/currentpage.html", "../finalpage.html") 
'http://www.asite.com/finalpage.html' 

用於複製和粘貼:

try: 
    from urlparse import urljoin # Python2 
except ImportError: 
    from urllib.parse import urljoin # Python3 
+0

對於RFC 3986和兼容Unicode編碼更換,請參閱[ uritools](http://pythonhosted.org/uritools/)。 – Marian 2016-02-22 09:43:38