Created
July 15, 2013 06:34
-
-
Save lepture/5997883 to your computer and use it in GitHub Desktop.
Clean utm parameters on link.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import urlparse | |
def linky(url): | |
"""Sanitize link. clean utm parameters on link.""" | |
if not re.match(r'^https?:\/\/', url): | |
url = 'http://%s' % url | |
rv = urlparse.urlparse(url) | |
if rv.query: | |
query = re.sub(r'utm_\w+=[^&]+&?', '', rv.query) | |
url = '%s://%s%s?%s' % (rv.scheme, rv.hostname, rv.path, query) | |
# remove ? at the end of url | |
url = re.sub(r'\?$', '', url) | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if we have url="https://www.idc.com/getdoc.jsp?containerId=prJPJ47694621&utm_medium=rss_feed&utm_source=alert&utm_campaign=rss_syndication"
it will not clean the url, "&" special char still there in url.
OR
Is there any way to do all regex subtraction in one expression.