I am trying to build a url so that I can send a get request to it using urllib
module.
Let's suppose my final_url
should be
url = "www.example.com/find.php?data=http%3A%2F%2Fwww.stackoverflow.com&search=Generate+value"
Now to achieve this I tried the following way:
>>> 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 the format of final_url
you can observer two things
1) The order of params are reversed instead of data=()&search=
it is search=()&data=
2) urlencode
also encoded the +
in Generate+value
I believe the first change is due to the random behaviour of dictionary. So, I though of using OrderedDict
to reverse the dictionary. As, I am using python 2.6.5
I did
pip install ordereddict
But I am not able to use it in my code when I try
>>> 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 what is the correct way to use OrderedDict
in python 2.6.5 and how do I make urlencode
ignores the +
in Generate+value
.
Also, is this the correct approach to build URL
.
from collections import OrderedDict
but now I am gettingImportError: cannot import name OrderedDict
. I am using python2.6.5
– Tillo