Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove utm_* parameters from URL in Python

Tags:

python

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?

like image 523
Kostas Di Avatar asked Jul 24 '12 22:07

Kostas Di


2 Answers

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'
like image 74
Jon Clements Avatar answered Oct 11 '22 14:10

Jon Clements


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'
like image 31
mVChr Avatar answered Oct 11 '22 13:10

mVChr