Best way to parse a URL query string Best way to parse a URL query string python python

Best way to parse a URL query string


Here is an example using python3 urllib.parse:

from urllib.parse import urlparse, parse_qsURL='https://someurl.com/with/query_string?i=main&mode=front&sid=12ab&enc=+Hello'parsed_url = urlparse(URL)parse_qs(parsed_url.query)

output:

{'i': ['main'], 'enc': [' Hello '], 'mode': ['front'], 'sid': ['12ab']}

Note for python2: from urlparse import urlparse, parse_qs

SEE: https://pythonhosted.org/six/#module-six.moves.urllib.parse


The urllib.parse module is your friend: https://docs.python.org/3/library/urllib.parse.html

Check out urllib.parse.parse_qs (parsing a query-string, i.e. form data sent to server by GET or form data posted by POST, at least for non-multipart data). There's also cgi.FieldStorage for interpreting multipart-data.

For parsing the rest of an HTTP interaction, see RFC2616, which is the HTTP/1.1 protocol specification.


If you need unique key from query string, use dict() with parse_qsl()

import urllib.parseurllib.parse.urlparse('https://someurl.com/with/query_string?a=1&b=2&b=3').query    a=1&b=2&b=3urllib.parse.parse_qs('a=1&b=2&b=3');    {'a': ['1'], 'b': ['2','3']}urllib.parse.parse_qsl('a=1&b=2&b=3')    [('a', '1'), ('b', '2'), ('b', '3')]dict(urllib.parse.parse_qsl('a=1&b=2&b=3'))    {'a': '1', 'b': '3'}