I am looking for a way to fully determine the URL using Python. I have the current page URL, for example:
http://www.foo.com/Stuff/Mike/Doc.html
and I have my href, for example:
href = "../Bob/doc.html"
I need to build:
http://www.foo.com/Stuff/Bob/Doc.html
Are there any libraries in Python that can parse such paths? I looked through the docs for urllib and urllib2, but couldn't find anything like it. Thank!
Use the library urlparse.
urlparse
>>> import urlparse >>> urlparse.urljoin("http://www.foo.com/Stuff/Mike/Doc.html","../Bob/Doc.html") 'http://www.foo.com/Stuff/Bob/Doc.html'
Additionally:
python 3, :
>>> from urllib.parse import urljoin >>> urlparse.urljoin("http://www.foo.com/Stuff/Mike/Doc.html","../Bob/Doc.html") 'http://www.foo.com/Stuff/Bob/Doc.html'