Python请求-通过GET传递参数 [英] Python Requests - pass parameter via GET

查看:46
本文介绍了Python请求-通过GET传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用API并将一些参数传递给它.

I am trying to call an API and pass some parameters to it.

我的端点应如下所示:

https://example.com?q=food 食物是参数

import requests
parametervalue = "food"
r = requests.get("https://example.com/q=", parametervalue)

当我打印r.url时,没有得到正确的结果-该值未传递.

When I do print r.url, I do not get the correct result - the value is not passed.

已更新:

错误:

  r = requests.get('https://example.com', params={'1':parametervalue})
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
  return request('get', url, **kwargs)

更新:

答案在下面发布.谢谢.

Answer posted below. Thank you.

注意:帖子中提到的SSL错误是由于我运行Python 2.7引起的.我使用python3并解决了该问题.

Note: The SSL error mentioned in the post was due to me running Python 2.7. I used python3 and it fixed the issue.

推荐答案

以下是相关代码,可从

Here's the relevant code to perform a GET http call from the official documentation

import requests
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.get('http://httpbin.org/get', params=payload)

为了使其适应您的特定要求:

In order to adapt it to your specific request:

import requests
payload = {'q': 'food'}
r = requests.get('http://httpbin.org/get', params=payload)
print (r.text)

如果运行第二个示例,这是获得的结果:

Here's the obtained result if I run the 2nd example:

python request_test.py
{"args":{"q":"food"},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Host":"httpbin.org","User-Agent":"python-requests/2.18.1"},"origin":"x.y.z.a","url":"http://httpbin.org/get?q=food"}

这篇关于Python请求-通过GET传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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