I am trying to create a url to send a receive request using the urllib
module.
Suppose my final_url
should be
url = "www.example.com/find.php?data=http%3A%2F%2Fwww.stackoverflow.com&search=Generate+value"
Now for this I tried as follows:
>>> initial_url = "http://www.stackoverflow.com" >>> search = "Generate+value" >>> params = {"data":initial_url,"search":search} >>> query_string = urllib.urlencode(params) >>> query_string 'search=Generate%2Bvalue&data=http%3A%2F%2Fwww.stackoverflow.com'
Now, if you compare my query_string
with final_url
format, you can observe two things
1) The order of the parameters is reversed, not data=()&search=
is equal to search=()&data=
2) urlencode
also encoded +
in Generate+value
I believe the first change is due to random dictionary behavior. So, I use OrderedDict
to undo the dictionary . Since, I am using python 2.6.5
, I did
pip install ordereddict
But I can not use it in my code when trying
>>> od = OrderedDict((('a', 'first'), ('b', 'second'))) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'OrderedDict' is not defined
So my question is how to use OrderedDict
in python 2.6.5 and how to make urlencode
ignore +
in Generate+value
.
Also, this is the right approach to building URL
.