一、接口必要的数据
请求地址:
请求方法:
请求参数:
响应参数
二、requests库的安装
pip install requests
三、request的使用
支持所有的http请求方式
1、requests 发送get请求
get请求:
def get(url, params=None, kwargs):
参数放在URL后面:http://httpbin.org/get?key1=value1&key2=value2
参数使用params关键字**接收,会自动拼接到url地址后面
?:表示携带的参数,多个参数使用&分开
import pprint
import requests
url = "http://httpbin.org/get"
data = {"key1": "value1", "key2": "value2"}
res = requests.get(url=url, params=data)
pprint.pprint(res.json())
控制台打印结果:
{'args': {'key1': 'value1', 'key2': 'value2'},
'headers': {'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate',
'Host': 'httpbin.org',
'User-Agent': 'python-requests/2.22.0',
'X-Amzn-Trace-Id': 'Root=1-61dbd58d-580c153e69496c56095615e5'},
'origin': '61.16.102.70',
'url': 'http://httpbin.org/get?key1=value1&key2=value2'}
2、requests 发送post请求
**def post(url, data=None, json=None, kwargs):
url:请求地址
data:请求参数 默认编码格式:form表单
json:请求参数 默认编码格式:json 如果设置请求头为form表单,那么使用设置的方式。
如果请求参数是data:
import requests
import pprint
url = "http://httpbin.org/post"
data = {"key1": "value1", "key2": "value2"}
res = requests.post(url=url, data=data)
pprint.pprint(res.json())
则返回结果就是以form表单的形式:
如果请求参数是json:
import requests
import pprint
url = "http://httpbin.org/post"
data = {"key1": "value1", "key2": "value2"}
res = requests.post(url=url, json=data)
pprint.pprint(res.json())
则返回结果就是以json格式:
如果请求参数使用的json,但是在请求头中设置了form表单的编码格式,那么返回的就是form表单
import requests
import pprint
url = "http://httpbin.org/post"
headers = {"Content-Type": "application/x-www-form-urlencoded"}
data = {"key1": "value1", "key2": "value2"}
res = requests.post(url=url, json=data, headers=headers)
pprint.pprint(res.json())
四、实际场景使用
1、session鉴权,去请求课堂派的登录接口
从响应中可获取:
1、从响应里面获取响应状态码
response.status.code
2、从响应里面获取cookies对象
response.cookies
3、将响应结果打印成json格式数据
response.json()
4、查看请求头信息
response.request.headers
5、查看请求地址
response.request.url
方式一:从登录接口返回的响应头中获取cookie,
import requests
import pprint
# 发送登录接口,从response中获取token,供其他接口请求
login_url = "https://v4.ketangpai.com/UserApi/login"
login_data = {"email": "1605118090@qq.com", "password": "Aa123456", "remember": "0"}
res = requests.post(url=login_url, data=login_data)
cookie = res.headers['Set-Cookie']
pprint.pprint(res.json())
is_vip_url = "https://v4.ketangpai.com/VipApi/isVip"
res2 = requests.get(url=is_vip_url, headers={"cookie": cookie})
pprint.pprint(res2.json())
方式二:
import requests
import pprint
# 发送登录接口,从response中获取token,供其他接口请求
login_url = "https://v4.ketangpai.com/UserApi/login"
login_data = {"email": "1605118090@qq.com", "password": "Aa123456", "remember": "0"}
res = requests.post(url=login_url, data=login_data)
is_vip_url = "https://v4.ketangpai.com/VipApi/isVip"
res2 = requests.get(url=is_vip_url, cookies=res.cookies)
pprint.pprint(res2.json())
方式三、自动缓存,推荐使用
import requests
from pprint import pprint
# 自动缓存session
session = requests.session()
login_url = "https://v4.ketangpai.com/UserApi/login"
login_data = {"email": "1605118090@qq.com", "password": "Aa123456", "remember": "0"}
res = session.post(url=login_url, data=login_data)
pprint(res.json())
is_vip_url = "https://v4.ketangpai.com/VipApi/isVip"
res2 = session.get(url=is_vip_url)
pprint(res2.json())
2、token鉴权
token:token通过什么方式返回给你,由后端决定
1、token:通过登录接口的response
2、token:通过一个专门的接口返回,放在响应头
import requests
from pprint import pprint
import time
now = int(time.time() * 1000)
login_url = "https://openapiv5.ketangpai.com/UserApi/login"
login_data = {"email": "1605118090@qq.com",
"password": "Aa123456",
"remember": "0",
"code": "",
"mobile": "",
"type": "login",
"reqtimestamp": now}
res = requests.post(url=login_url, json=login_data)
pprint(res.json())
# 从登录接口返回的response中获取token
token = res.json()["data"]["token"]
# 把token传给其他接口作为请求头参数去请求
headers = {"token": token}
url = "https://openapiv5.ketangpai.com/UserApi/getUserBasinInfo"
data = {"reqtimestamp": now}
res2 = requests.post(url=url, json=data, headers=headers)
print(res2.json())
控制台打印:
{'code': 10000,
'data': {'isenterprise': 0,
'token': 'd103a38f3d6f9f634c39c0a91d7e11a6dbd89f5e96e9ed5296b0b7a97f29cb3b',
'uid': 'MDAwMDAwMDAwMLScz5aHub-whaVyoQ',
'url': ''},
'message': '访问成功',
'status': 1}
{'status': 1, 'code': 10000, 'message': '访问成功', 'data': {'usertype': '0', 'username': '海励', 'account': 'ktp0216965148', 'avatar': 'http://assets.ketangpai.com//Public/Common/img/40/40.png', 'stno': '10086', 'school': '柠檬学员', 'email': '1605118090@qq.com', 'mobile': None, 'department': None, 'teachcourseid': None, 'isenterprise': 1, 'coid': None, 'token': 'd103a38f3d6f9f634c39c0a91d7e11a6dbd89f5e96e9ed5296b0b7a97f29cb3b', 'oldtoken': 'MDAwMDAwMDAwMMurrpWavLehhs1-lrO5kZKEzXeUepuomcWmmqaMiHtnr5ylzYWosKKZq6HQxtOK0ZCme5p-q6iZu2yrn4uNhJ3KedDYk7ivboS4jpSxuZ_Yg92L24B6YW0', 'uid': 'MDAwMDAwMDAwMLScz5aHub-whaVyoQ', 'isvip': 0, 'setting': '', 'mobile_url': 'https://w.ketangpai.com'}}