cooke

#cookie主要放在本地浏览器,存在客户端
'''
r.cookies#获取cookies
requests.utils.dict_from_cookiejar(r.cookies)#jar包转化为字典
requests.utils.cookiejar_from_dict()#字典转化为jar包
r = requests.get(url,cookies=cookies)#发送cookies
添加cookies的方法
s.cookies.set("cookie-name","cookie-value",path="/",domain=".abc.com")
requests.utils.add_dict_to_cookiejar(s.cookies.cookiesdict)#缺点:不能添加path和domain
保持Session会话
'''
#session主要放在服务器端

import requests
url = "http://httpbin.org/cookies"
url1 = "http://www.baidu.com/"

r = requests.get(url1)
print(r.cookies)#获取cookies
print(r.text)
d = requests.utils.dict_from_cookiejar(r.cookies)#jar包转化为字典
#requests.utils.cookiejar_from_dict()#字典转化为jar包
print(d)
print({a.name:a.value for a in r.cookies})

#发送cookies到服务器
cookies = {"cookie-name":"qiaoba"}
r1 = requests.get(url,cookies=cookies)#发送cookies
print(r1.text)

#复杂的写法
s = requests.Session()#保持Session会话
c = requests.cookies.RequestsCookieJar()
c.set("cookie-name","cookie-value",path="/",domain=".text.com")
s.cookies.update(c)
print(s.cookies)
posted @ 2018-12-24 23:36  测试小白学习之路  阅读(419)  评论(0编辑  收藏  举报