Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…


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:

url = 'http://www.hackviking.com/?var=value'
url = url[:url.find('?')]

url now reads 'http://www.hackviking.com/'

Posted by Kristofer Källsbo

Comments (3) Trackbacks (0)
  1. 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]

  2. this also helps url with no queries
    url[:url.find(‘?’)] if ‘?’ in url else url

  3. I think The best should be url.split(‘?’,maxsplit=1)[0]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

No trackbacks yet.