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/’

3 Comments on “Python: Remove querystring from URL”

  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]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: