6

I'm looking for a way to fully qualify a URL using Python. I have my current page URL, such as:

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

and I have my href, such as:

href="../Bob/Doc.html"

What I need to build is:

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

Does Python have any libraries that can parse paths like this? I looked around the docs for urllib and urllib2, but couldn't find anything like that. Thanks!

1

2 Answers 2

10

Use the urlparse library.

>>> import urlparse
>>> urlparse.urljoin("http://www.foo.com/Stuff/Mike/Doc.html","../Bob/Doc.html")
'http://www.foo.com/Stuff/Bob/Doc.html'
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! I knew this would turn out to be ridiculously easy in Python.
3

For additional:

If you are using python 3, The library name has changed:

>>> from urllib.parse import urljoin
>>> urljoin("http://www.example.com/Stuff/Mike/Doc.html","../Bob/Doc.html")
'http://www.example.com/Stuff/Bob/Doc.html'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.