I'm trying to write a script that can parse a url and get the query params from it. So far I've mostly got it, but urllib.urlencode is adding square brackets and single quotes to my values when I think it shouldn't be. This is most likely a misunderstanding on my part, so if someone could explain why this happens and how to avoid it I would really appreciate it. Here is my script:
#/usr/bin/python
import urlparse
import urllib
url = 'https://mysite.com?Action=ParseUrl'
parsed_url = urlparse.urlparse(url)
query_params = urlparse.parse_qs(parsed_url.query)
query_string = urllib.urlencode(query_params)
print query_string
The resulting output is:
Action=%5B%27ParseUrl%27%5D
So you can see that the value of 'Action' has been surrounded by urlencoded [' '] characters. I would like the value of my query_string variable to be :
Action=ParseUrl
What can I do to my script to make this happen. I realize I could hack some sort of reg exp to remove the characters, but I would rather understand the fundamental reason of why this is not working the way I want it to so I can avoid this problem in the future.
Thanks!
parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.
What does Urllib quote do? The quote() function encodes space characters to %20 . If you want to encode space characters to plus sign ( + ), then you can use another function named quote_plus provided by urllib.
urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.
See the documentation notes about parse_qs
(emphasis added):
Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.
You can confirm this yourself:
>>> print query_params
{'Action': ['ParseUrl']}
Consider using parse_qsl
instead. With that change, the rest of your script behaves as you intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With