I've been trying to remove all utm_* parameters from a list of URLs. The closest thing I have found is this: https://gist.github.com/626834.
Any ideas?
It's a bit long but uses the url* modules, and avoids re's.
from urllib import urlencode
from urlparse import urlparse, parse_qs, urlunparse
url = 'http://whatever.com/somepage?utm_one=3&something=4&utm_two=5&utm_blank&something_else'
parsed = urlparse(url)
qd = parse_qs(parsed.query, keep_blank_values=True)
filtered = dict( (k, v) for k, v in qd.iteritems() if not k.startswith('utm_'))
newurl = urlunparse([
parsed.scheme,
parsed.netloc,
parsed.path,
parsed.params,
urlencode(filtered, doseq=True), # query string
parsed.fragment
])
print newurl
# 'http://whatever.com/somepage?something=4&something_else'
import re
from urlparse import urlparse, urlunparse
url = 'http://www.someurl.com/page.html?foo=bar&utm_medium=qux&baz=qoo'
parsed_url = list(urlparse(url))
parsed_url[4] = '&'.join(
[x for x in parsed_url[4].split('&') if not re.match(r'utm_', x)])
utmless_url = urlunparse(parsed_url)
print utmless_url # 'http://www.someurl.com/page.html?foo=bar&baz=qoo'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With