session对象能够跨http请求保持某些参数
import requests
s = requests.Session()
#设置cookies
s.get("http://httpbin.org/cookies/set/sessioncookie/123456789")
#发送请求,查看当前请求的cookies
r = s.get("http://httpbin.org/cookies")
print(r.text)
运行结果
{
"cookies": {
"sessioncookie": "123456789"
}
}
单独的请求携带临时参数
import requests
s = requests.Session()
s.headers.update({'key1': 'value1'})
r1 = s.get('http://httpbin.org/headers', headers={'key2': 'value2'})
print(r1.text)
r2 = s.get('http://httpbin.org/headers')
print(r2.text)
运行结果
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"Key1": "value1",
"Key2": "value2",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-6583e501-43e109f1010acd8e2e053fd1"
}
}
{
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"Key1": "value1",
"User-Agent": "python-requests/2.31.0",
"X-Amzn-Trace-Id": "Root=1-6583e502-7d6ff35a61dee8f9048151ec"
}
}
标签:cookies,httpbin,http,headers,session,模块,org,requests
From: https://www.cnblogs.com/taowa/p/17919129.html