cookie and session requests library

cookie

If a corresponding contains a cookie, the cookie can be used then to get the property returned cookie value:

res = requests.get('http://www.baidu.com')
print(res.cookies)
print(res.cookies.get_dict())

 session

Before using urllib library can use the opener to send multiple requests can be shared among multiple requests cookie, then if you use requests, but also to achieve the purpose of sharing cookie, you can use the session object requests the library to us. Note, session here is not web development in that session, this place is just a session object only, or to log all network, for example, the use of requests to achieve. Sample code is as follows:

url = 'http://www.renren.com/PLogin.do'
dapeng_url = 'http://www.renren.com/880151247/profile'
data = {
    'email': '15666809216',
    'password': 'beauty234',
}
headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
}
# log in
session = requests.session()
session.post(url=url, data=data, headers=headers)
res = session.get(dapeng_url)
print(res.text)

 

Guess you like

Origin www.cnblogs.com/wangyue0925/p/11094162.html