python删除代码中的参数_从Python中的URL中删除utm_x参数

很简单,很管用,基于你发布的链接,但它是。。。所以,我不确定它是否会因为某种我想不到的原因而破裂:)import re

def trim_utm(url):

if "utm_" not in url:

return url

matches = re.findall('(.+\?)([^#]*)(.*)', url)

if len(matches) == 0:

return url

match = matches[0]

query = match[1]

sanitized_query = '&'.join([p for p in query.split('&') if not p.startswith('utm_')])

return match[0]+sanitized_query+match[2]

if __name__ == "__main__":

tests = [ "http://localhost/index.php?a=1&utm_source=1&b=2",

"http://localhost/index.php?a=1&utm_source=1&b=2#hash",

"http://localhost/index.php?a=1&utm_source=1&b=2&utm_something=no#hash",

"http://localhost/index.php?a=1&utm_source=1&utm_a=yes&b=2#hash",

"http://localhost/index.php?utm_a=a",

"http://localhost/index.php?a=utm_a",

"http://localhost/index.php?a=1&b=2",

"http://localhost/index.php",

"http://localhost/index.php#hash2"

]

for t in tests:

trimmed = trim_utm(t)

print t

print trimmed

print

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值