How can I get the base of a URL in Python?

前端 未结 8 2470
情书的邮戳
情书的邮戳 2021-02-12 12:34

I\'m trying to determine the base of a URL, or everything besides the page and parameters. I tried using split, but is there a better way than splitting it up into pieces? Is th

8条回答
  •  醉梦人生
    2021-02-12 13:06

    Well, for one, you could just use os.path.dirname:

    >>> os.path.dirname('http://127.0.0.1/asdf/login.php')
    'http://127.0.0.1/asdf'
    

    It's not explicitly for URLs, but it happens to work on them (even on Windows), it just doesn't leave the trailing slash (you can just add it back yourself).

    You may also want to look at urllib.parse.urlparse for more fine-grained parsing; if the URL has a query string or hash involved, you'd want to parse it into pieces, trim the path component returned by parsing, then recombine, so the path is trimmed without losing query and hash info.

    Lastly, if you want to just split off the component after the last slash, you can do an rsplit with a maxsplit of 1, and keep the first component:

    >>> 'http://127.0.0.1/asdf/login.php'.rsplit('/', 1)[0]
    'http://127.0.0.1/asdf'
    

提交回复
热议问题