1

I wrote the following code to extract Get parameters from url like this:

from urllib.parse import urlparse
from urllib.parse import parse_qs

url = 'https://www.example.com/some_path?some_key=some_value&tt=new_value'
parsed_url = urlparse(url)
for val in parse_qs(parsed_url.query):
    print(val)

it works great with the above url but once I do url-encoding (which is still a valid url scheme as browsers accept it) like this:

https://www.example.com/some_path?some_key%3Dsome_value%26tt%3Dnew_value

My code doesn't output anything, why is that and how to fix?

Note: I know some of you may suggest doing url-decoding for the input but I don't think this will solve all cases, what if there was another encoding like for urls which is valid and should be parsed correctly?

zoro
  • 15
  • 5

1 Answers1

0

parse_qs decodes %-encoding in names and values, but it requires a literal = between query names and values and (by default) a literal & as separator. You can check this in the source here - parse_qsl does the work, and for example it does a string split on = to separate query names and values.

From your last sentence I don't think it's what you want, but parse_qs(urllib.parse.unquote(parsed_url.query)) would work.