使用请求在 Python 中查询字符串数组参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22974772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:04:20  来源:igfitidea点击:

Querystring Array Parameters in Python using Requests

pythonpython-requests

提问by Buddy Lindsey

I have been trying to figure out how to use python-requeststo send a request that the url looks like:

我一直在试图弄清楚如何使用python-requests发送 url 如下所示的请求:

http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'

Normally I can build a dictionary and do:

通常我可以建立一个字典并执行以下操作:

data = {'name': 'hello', 'data': 'world'}
response = requests.get('http://example.com/api/add.json', params=data)

That works fine for most everything that I do. However, I have hit the url structure from above, and I am not sure how to do that in python without manually building strings. I can do that, but would rather not.

这对我所做的大多数事情都很好。但是,我已经从上面点击了 url 结构,我不确定如何在没有手动构建字符串的情况下在 python 中做到这一点。我可以这样做,但宁愿不这样做。

Is there something in the requests library I am missing or some python feature I am unaware of?

请求库中是否有我遗漏的内容或我不知道的某些 Python 功能?

Also what do you even call that type of parameter so I can better google it?

另外你甚至称这种类型的参数是什么,所以我可以更好地谷歌它?

采纳答案by Arvind

What u are doing is correct only. The resultant url is same what u are expecting.

你所做的只是正确的。结果 url 与您期望的相同。

>>> payload = {'name': 'hello', 'data': 'hello'}
>>> r = requests.get("http://example.com/api/params", params=payload)

u can see the resultant url:

你可以看到结果网址:

>>> print(r.url)
http://example.com/api/params?name=hello&data=hello

According to url format:

根据网址格式

In particular, encoding the query string uses the following rules:

特别是,对查询字符串进行编码使用以下规则:

  • Letters (A–Z and a–z), numbers (0–9) and the characters .,-,~and _are left as-is
  • SPACE is encoded as +or %20
  • All other characters are encoded as %HH hex representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)
  • 字母(A–Z 和 a–z)、数字 (0–9) 和字符., -,~_保持原样
  • SPACE 被编码为+%20
  • 所有其他字符都编码为 %HH 十六进制表示,任何非 ASCII 字符首先编码为 UTF-8(或其他指定的编码)

So array[]will not be as expected and will be automatically replaced according to the rules:

所以 array[]不会像预期的那样,会根据规则自动替换:

If you build a url like :

如果你建立一个像这样的网址:

`Build URL: http://example.com/api/add.json?name='hello'&data[]='hello'&data[]='world'`

OutPut will be:

输出将是:

>>> payload = {'name': 'hello', "data[]": 'hello','data[]':'world'}
>>> r = requests.get("http://example.com/api/params", params=payload)
>>> r.url
u'http://example.com/api/params?data%5B%5D=world&name=hello'

This is because Duplication will be replaced by the last value of the key in url and data[]will be replaced by data%5B%5D.

这是因为 Duplication 将被 url 中键的最后一个值data[]替换,并将被替换为data%5B%5D.

If data%5B%5Dis not the problem(If server is able to parse it correctly),then u can go ahead with it.

如果data%5B%5D不是问题(如果服务器能够正确解析),那么您可以继续进行。

Source Link

来源链接

回答by Tomer Zait

All you need to do is putting it on a list and making the key as list like string:

您需要做的就是将它放在一个列表中,并将键作为像 string一样的列表

data = {'name': 'hello', 'data[]': ['hello', 'world']}
response = requests.get('http://example.com/api/add.json', params=data)

回答by sanchitarora

One solution if using the requests module is not compulsory, is using the urllib/urllib2combination:

如果使用请求模块不是强制性的,一种解决方案是使用urllib/urllib2组合:

payload = [('name', 'hello'), ('data[]', ('hello', 'world'))]
params = urllib.urlencode(payload, doseq=True)
sampleRequest = urllib2.Request('http://example.com/api/add.json?' + params)
response = urllib2.urlopen(sampleRequest)

Its a little more verbose and uses the doseq(uence) trick to encode the url parameters but I had used it when I did not know about the requests module.

它有点冗长,并使用了剂量(uence)技巧来编码 url 参数,但是当我不知道 requests 模块时我使用了它。

For the requests module the answer provided by @Tomer should work.

对于请求模块,@Tomer 提供的答案应该有效。

回答by bnik

Some api-servers expect json-array as value in the url query string. The requests params doesn't create json array as value for parameters.

一些 api 服务器期望 json-array 作为 url 查询字符串中的值。请求参数不会创建 json 数组作为参数值。

The way I fixed this on a similar problem was to use urllib.parse.urlencode to encode the query string, add it to the url and pass it to requests

我在类似问题上解决这个问题的方法是使用 urllib.parse.urlencode 对查询字符串进行编码,将其添加到 url 并将其传递给请求

e.g.

例如

from urllib.parse import urlencode
query_str = urlencode(params)
url = "?" + query_str
response = requests.get(url, params={}, headers=headers)