Parse query part from url
Asked Answered
C

1

22

I want to parse query part from url, this is my code to do this:

>>> from urlparse import urlparse, parse_qs
>>> url = '/?param1&param2=2'
>>> parse_qs(urlparse(url).query)
>>> {'param2': ['23']}

This code looks good, but "parse_qs" method loses query parameters like "param1" or "param1=". Can I parse query part with stantard library and save all parameters?

Cristiecristin answered 22/7, 2012 at 13:30 Comment(1)
Does this answer your question? Retrieving parameters from a URLSimplehearted
B
54

You want:

from urlparse import parse_qs, urlparse

parse_qs(urlparse(url).query, keep_blank_values=True)
# {'param2': ['2'], 'param1': ['']}
Bolero answered 22/7, 2012 at 13:41 Comment(1)
For Python 3 the imports are from urllib.parse import parse_qs, urlparseUltimo

© 2022 - 2024 — McMap. All rights reserved.