使用urlencode python构建查询字符串 [英] Build query string using urlencode python

查看:159
本文介绍了使用urlencode python构建查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个网址,以便我可以使用 urllib 模块发送获取请求。



让我们假设我的 final_url 应该是

  url =www。 example.com/find.php?data=http%3A%2F%2Fwww.stackoverflow.com&search=Generate+value

现在,我尝试了以下方式:

 >>> initial_url =http://www.stackoverflow.com
>>> search =生成+值
>>> params = {data:initial_url,search:search}
>>> query_string = urllib.urlencode(params)
>>> query_string
'search =生成%2Bvalue& data = http%3A%2F%2Fwww.stackoverflow.com'

现在,如果比较我的 query_string 格式为 final_url ,您可以观察两件事>

1)参数的顺序是颠倒的,而不是 data =()& search = 它是 search =()& data =



2) urlencode 也编码 + 生成+值



我相信第一更改是由字典的随机行为造成的。所以,尽管我使用 OrderedDict 来扭转字典。因为,我使用 python 2.6.5 我做了

  pip install但是当我尝试

$ b时,我无法在我的代码中使用它。
$ b

 >>> od = OrderedDict(('a','first'),('b','second')))
追溯(最近的最后一次调用):
文件< stdin>,第1行,< module>
NameError:name'OrderedDict'未定义

所以,我的问题是什么在python 2.6.5中使用 OrderedDict 的正确方法,如何使 urlencode 忽略 + 生成+值



此外,这是正确的方法来构建 URL

解决方案

您不必担心编码 + 应该在解压缩URL后在服务器上恢复。命名参数的顺序也不重要。



考虑到OrderedDict,它不是Python的内置的,你应该从 code>

 从urllib导入urlencode,引用
从集合导入OrderedDict

initial_url =http://www.stackoverflow.com
search =生成+值
query_string = urlencode(OrderedDict(data = initial_url,search = search))

如果您的python太旧,并且模块中没有OrderedDict 收藏,使用:

  encoded =&join(%s = s%(key,quote(parameters [key],safe =+))
为有序键(parameters.keys()))

无论如何,参数的顺序应该不重要。



请注意 safe 参数报价。它可以防止 + 被转义,但这意味着服务器会将 Generate + value 解释为生成值。您可以通过编写%2B 并标记% + >作为安全字符:


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.

解决方案

You shouldn't worry about encoding the + it should be restored on the server after unescaping the url. The order of named parameters shouldn't matter either.

Considering OrderedDict, it is not Python's built in. You should import it from collections:

from urllib import urlencode, quote
from collections import OrderedDict

initial_url = "http://www.stackoverflow.com"
search = "Generate+value"
query_string = urlencode(OrderedDict(data=initial_url,search=search))

if your python is too old and does not have OrderedDict in the module collections, use:

encoded = "&".join( "%s=%s" % (key, quote(parameters[key], safe="+")) 
    for key in ordered(parameters.keys()))

Anyway, the order of parameters should not matter.

Note the safe parameter of quote. It prevents + to be escaped, but it means , server will interpret Generate+value as Generate value. You can manually escape + by writing %2Band marking % as safe char:

这篇关于使用urlencode python构建查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆