英文:
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="name","numer"&where=number="10"
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="name","numer"`. This passes a single value as the `select` parameter, namely the string `'"name","numer"'`. Yet in your `params` dictionary you have for the `select` parameter `["name", "numer"]`, which would be passing a list of two values as if you had specified `?select=name&select=numer`. So it's not clear what it is you really wish to pass. But if I go by your original URL, then:
```python
params = {"select": '"name","numer"', "where": 'number="10"'}
url = "http://somepage.com"
# 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="name","numer"
. This passes a single value as the select
parameter, namely the string '"name","numer"'
. Yet in your params
dictionary you have for the select
parameter ["name", "numer"]
, which would be passing a list of two values as if you had specified ?select=name&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 = {"select": '"name","numer"', "where": 'number="10"'}
url = "http://somepage.com"
# 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 = {"select": ["name", "numer"], "where": {"number": "5"}}
url = "http://somepage.com"
response = requests.get(url, params=params)
print(response.text)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论