如何使用request.get传递参数而不是完整路径

huangapple go评论63阅读模式
英文:

How can I use request.get with paramerer instead of full path

问题

我有一个页面我想从中获取数据和示例请求调用例如 http://smepage.com?select="name","numer"&where=number="10"

要获取数据我使用 Python 的请求模块它可以正常工作
```python
URL=http://somepage.com?select="name","numer"&where=number="10"
requests.get(URL)

但是是否可以使用字典或其他容器来传递参数呢?
我尝试使用 JSON 但它没有起作用:

params = {
"select":["name","numer"],
"where": {"number":"5"}
}
URL=http://somepage.com
requests.get(URL, json=params)

<details>
<summary>英文:</summary>

I have a page from which I would like to get data and sample request call  e.g. http://smepage.com?select=&quot;name&quot;,&quot;numer&quot;&amp;where=number=&quot;10&quot;

To get the data I use python requests and it works:

URL=http://somepage.com?select="name","numer"&where=number="10"
requests.get(URL)

But is it possible to use dict or some other container to pass parameters?
I tried to use json but it did not work

params = {
"select":["name","numer"],
"where": {"number":"5"}
}
URL=http://somepage.com
requests.get(URL, json=params)


</details>


# 答案1
**得分**: 1

You have has part of your query `?select=&quot;name&quot;,&quot;numer&quot;`. This passes a single value as the `select` parameter, namely the string `&#39;&quot;name&quot;,&quot;numer&quot;&#39;`. Yet in your `params` dictionary you have for the `select` parameter `[&quot;name&quot;, &quot;numer&quot;]`, which would be passing a list of two values as if you had specified `?select=name&amp;select=numer`. So it&#39;s not clear what it is you really wish to pass. But if I go by your original URL, then:

```python
params = {&quot;select&quot;: &#39;&quot;name&quot;,&quot;numer&quot;&#39;, &quot;where&quot;: &#39;number=&quot;10&quot;&#39;}
url = &quot;http://somepage.com&quot;
# params=params, not json=params:
response = requests.get(url, params=params)
print(response.text)

You need to specify for params a dictionary whose keys are the parameter names and whose values are either a string or list of strings and then pass this dictionary to get as the params argument.

英文:

You have has part of your query ?select=&quot;name&quot;,&quot;numer&quot;. This passes a single value as the select parameter, namely the string &#39;&quot;name&quot;,&quot;numer&quot;&#39;. Yet in your params dictionary you have for the select parameter [&quot;name&quot;, &quot;numer&quot;], which would be passing a list of two values as if you had specified ?select=name&amp;select=numer. So it's not clear what it is you really wish to pass. But if I go by your original URL, then:

params = {&quot;select&quot;: &#39;&quot;name&quot;,&quot;numer&quot;&#39;, &quot;where&quot;: &#39;number=&quot;10&quot;&#39;}
url = &quot;http://somepage.com&quot;
# params=params, not json=params:
response = requests.get(url, params=params)
print(response.text)

You need to specify for params a dictionary whose keys are the parameter names and whose values are either a string or list of strings and then pass this dictionary to get as the params argument.

答案2

得分: 0

尝试在requests.get调用中使用命名参数params传递查询参数。

这段代码应该实现你想要的功能。

import requests

params = {"select": ["name", "numer"], "where": {"number": "5"}}
url = "http://somepage.com"
response = requests.get(url, params=params)
print(response.text)
英文:

Try passing the query parameters using the named argument params in the requests.get call.

This code should do what you want.

import requests


params = {&quot;select&quot;: [&quot;name&quot;, &quot;numer&quot;], &quot;where&quot;: {&quot;number&quot;: &quot;5&quot;}}
url = &quot;http://somepage.com&quot;
response = requests.get(url, params=params)
print(response.text)

huangapple
  • 本文由 发表于 2023年5月7日 18:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193468.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定