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

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

    Get the right-most occurence of slash; use the string slice through that position in the original string. The +1 gets you that final slash at the end.

    link = "http://127.0.0.1/asdf/login.php"
    link[:link.rfind('/')+1]
    

提交回复
热议问题