![](/img/trans.png)
[英]Not able to call post method using python requests library when using headers
[英]Using headers with the Python requests library's get method
所以我最近偶然发现了这个很棒的库,用于处理 Python 中的 HTTP 请求; 在这里找到http://docs.python-requests.org/en/latest/index.html 。
我喜欢使用它,但我不知道如何将标头添加到我的获取请求中。 帮助?
根据API ,标头都可以使用requests.get
传入:
import requests
r=requests.get("http://www.example.com/", headers={"content-type":"text"})
根据您链接的页面上的文档(强调我的),看起来很简单。
requests.get(url, params=None, headers=None, cookies=None, auth=None, timeout=None)
发送 GET 请求。 返回
Response
object。参数:
- url – URL 用于新
Request
object。- params –(可选)与
Request
一起发送的 GET 参数字典。- headers - (可选)HTTP 标题字典与
Request
一起发送。- cookies –(可选)CookieJar object 与
Request
一起发送。- auth –(可选)AuthObject 以启用基本 HTTP Auth。
- timeout –(可选)描述请求超时的浮点数。
这个答案告诉我,您可以为整个 session 设置标题:
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
奖励: 会话还处理 cookies。
headers = {'Accept-Language': content-copied-from-myhttpheader, 'User-Agent':content-copied-from-myhttpheader}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.