Best way to get query string from a URL in python? Best way to get query string from a URL in python? python python

Best way to get query string from a URL in python?


You can make Query string using GET parameters like this

request.GET.urlencode()

This does not include the ? prefix, and it may not return the keys in the same order as in the original request.


Third option:

>>> from urlparse import urlparse, parse_qs>>> url = 'http://something.com?blah=1&x=2'>>> urlparse(url).query'blah=1&x=2'>>> parse_qs(urlparse(url).query){'blah': ['1'], 'x': ['2']}

In Python 3+ this is available as:

from urllib.parse import parse_qs

Documentation for urllib.parse