【问题标题】:Convert relative URL to fully qualified URL using Python使用 Python 将相对 URL 转换为完全限定 URL
【发布时间】:2011-11-19 21:24:01
【问题描述】:

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

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

我有我的href,比如:

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

我需要构建的是:

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

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

【问题讨论】:

标签: python string http url


【解决方案1】:

补充:

如果您使用的是 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'

【讨论】:

    【解决方案2】:

    使用urlparse 库。

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

    【讨论】:

    • 不错!我知道这在 Python 中会变得非常简单。
    猜你喜欢
    • 2010-09-12
    • 2011-10-24
    • 1970-01-01
    • 2014-12-12
    • 2013-06-29
    • 2021-07-28
    • 1970-01-01
    • 2019-05-10
    相关资源
    最近更新 更多