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

前端 未结 8 2608
情书的邮戳
情书的邮戳 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:11

    No need to use a regex, you can just use rsplit():

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

提交回复
热议问题