如何从网址中删除查询字符串? [英] How to remove query string from a url?

查看:30
本文介绍了如何从网址中删除查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网址:

https://stackoverflow.com/questions/7990301?aaa=aaahttps://stackoverflow.com/questions/7990300?fr=aladdinhttps://stackoverflow.com/questions/22375#6https://stackoverflow.com/questions/22375?https://stackoverflow.com/questions/22375#3_1

我需要网址,例如:

https://stackoverflow.com/questions/7990301https://stackoverflow.com/questions/7990300https://stackoverflow.com/questions/22375https://stackoverflow.com/questions/22375https://stackoverflow.com/questions/22375

我的尝试:

url='https://stackoverflow.com/questions/7990301?aaa=aaa'如果 '?'在网址中:url=url.split('?')[0]如果 '#' 在 url:url = url.split('#')[0]

我认为这是一种愚蠢的方式

解决方案

非常有用的库 furl 使得删除查询和片段部分很简单:

<预><代码>>>>furl.furl("https://hi.com/?abc=def#ghi").remove(args=True, fragment=True).urlhttps://hi.com/

I have the following URL:

https://stackoverflow.com/questions/7990301?aaa=aaa
https://stackoverflow.com/questions/7990300?fr=aladdin
https://stackoverflow.com/questions/22375#6
https://stackoverflow.com/questions/22375?
https://stackoverflow.com/questions/22375#3_1

I need URLs for example:

https://stackoverflow.com/questions/7990301
https://stackoverflow.com/questions/7990300
https://stackoverflow.com/questions/22375
https://stackoverflow.com/questions/22375
https://stackoverflow.com/questions/22375

My attempt:

url='https://stackoverflow.com/questions/7990301?aaa=aaa'
if '?' in url:
    url=url.split('?')[0]
if '#' in url:
    url = url.split('#')[0]

I think this is a stupid way

解决方案

The very helpful library furl makes it trivial to remove both query and fragment parts:

>>> furl.furl("https://hi.com/?abc=def#ghi").remove(args=True, fragment=True).url
https://hi.com/

这篇关于如何从网址中删除查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆