Python: Remove querystring from URL
Needed to clean an URL from it’s querystring in Python. Found a lot of examples telling me to use urlparse and then put all the bits and pieces back together. An easier and more efficient way is this:
[py]url = ‘http://www.hackviking.com/?var=value’
url = url[:url.find(‘?’)][/py]
url now reads ‘http://www.hackviking.com/’
Thanks for sharing it!
But it doesn’t work if the uri has no query string.
I’m using this:
uri = uri.rsplit(‘?’, 1)[0]
LikeLike
this also helps url with no queries
url[:url.find(‘?’)] if ‘?’ in url else url
LikeLike
I think The best should be url.split(‘?’,maxsplit=1)[0]
LikeLike