python request库学习 五文件上传、获取cookies、会话维持
作者:互联网
––coding: utf-8 ––
import requests
#1.文件上传
files={‘file’:open(‘favicon.ico’,‘rb’)} #r 打开和读取,b 二进制模式
response=requests.post(‘http://httpbin.org/post’,files=files)
print(resposne.text)
#2.获取cookies
response = requests.get(‘https://www.baidu.com’)
print(response.cookies)
for key,value in response.cookies.items(): #获取cookies的key和vakue
print(key + ‘=’ + value)
#3.会话维持
#模拟登陆
request.get(‘http://httpbin.org/cookies/set/number/123456789’)
response = requests.get(‘http://httpbin.org/cookies’)
print(response.text) # 你会发现获取到的cookies是空的,因为这两个发起了两个独立的请求,所以会导致结果为空
#通过session对象发起两次请求
s =requests.Session() #创建一个session对象
s.get(‘http://httpbin.org/cookies/set/number/123456789’) #用对象储存cookies
response = s.get(‘http://httpbin.org/cookies’)
print(response.text)
标签:cookies,httpbin,get,python,request,print,http,response 来源: https://blog.csdn.net/weixin_43675653/article/details/117067454