开发者

Convert relative URL to fully qualified URL using Python

开发者 https://www.devze.com 2023-04-05 23:12 出处:网络
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

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.ht开发者_运维技巧ml

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!


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'


For additional:

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

>>> 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'
0

精彩评论

暂无评论...
验证码 换一张
取 消