一、安装requests模块
安装命令:pip install requests
二、python发送HTTP请求
import requests# 登录的接口地址url = 'http://............/login'# 登录的参数params = {"mobile_phone": 18310443005,"pwd": 12345678}# 请求头headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}# 发送登录请求# 请求类型为 Content-Type:Application/json,参数就应该使用json去传递response = requests.post(url=url, json=params, headers=headers)print(response) # 响应对象print(response.text) # 获取接口返回的数据
运行结果:<Response [200]>
{"code":0,"msg":"OK","data":{"id":2074458,"leave_amount":0.0,"mobile_phone":"18310443005","reg_name":"小柠檬","reg_time":"-08-12 02:05:32.0","type":1,"token_info":{"token_type":"Bearer","expires_in":"-08-12 17:31:48","token":"eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjIwNzQ0NTgsImV4cCI6MTU5NzIyNDcwOH0.MXlkib8YpNHudBhsdsutsyCFtd5XW53xKK_LYAs9wVVbvrEJxBw1uKzVZFW8Ze--RemKpzEQwh2ru0T3TUtwPw"}},"copyright":"Copyright 柠檬班 © - 湖南省零檬信息技术有限公司 All Rights Reserved"}
三、(重要)requests处理常见的接口请求参数类型
1、查询字符串参数:
1)常用于get请求(其他的请求方法用的少)
2)参数会直接拼接在url地址后面
3)requests发送请求,传递查询字符串参数,要使用params
import requestsurl = 'http://http://............/'# 请求参数params = {'pageIndex': 1,'pageSize': 20}# 请求头headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.get(url=url, params=params, headers=headers)print(response.text)
2、json类型的参数:
1)请求类型为 Content-Type:Application/json
2)requests发送请求,传递json参数,就应该使用json去传递
import requestsurl = 'http://............/member/register'# 请求参数params = {"mobile_phone":18310443008,"pwd":12345678}# 请求头headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.post(url=url, json=params, headers=headers)print(response.text)
3、表单类型的参数:
1)请求参数类型:content-type: application/x-www-form-urlencoded*
2)requests发送请求,传递表单参数,应该使用data去传递
import requests# 课堂派登录接口url = 'https://............../UserApi/login'# 请求参数params = {"email":18310443008,"password":12345678,'remember':0}# 请求头headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.post(url=url, data=params)print(response.json())
4、文件上传
1)请求参数类型:content-type:application/form-data(postman请求参数也选择这个)
2)文件参数要使用files进行传递
3)文件参数的组装:两种形式****
{“参数名”:(“文件名”,open以rb模式打开文件,“文件类型”)}
{(“参数名”,(“文件名”,open以rb模式打开文件,“文件类型”))}
import requests# 老师本地的上传文件接口地址url = 'http://127.0.0.1:5000/upload'# 请求参数params = {"nickname":'1122',"age":18,'sex':'男'}file = {'pic':('jiayouya.gif',open('jiayouya.gif','rb'),'image/gif')}response = requests.post(url=url, data=params, files = file)print(response.json())
四、返回的数据提取(主要使用第三种)
1)text属性(str):获取的是原生的json字符串(有可能出现乱码)****
2)content属性(bytes):可以使用decode指定编码转换为字符串(可以解决乱码问题)
3)json()方法(dict):获取到的是json转换的字典/列表(自动转换成python数据,使用起来更方便)
五、json类型数据和python数据的对比
六、jsonpath提取数据和json数据
1、安装:pip install jsonpath
2、通过字典键值对的方式获取token
import requestsurl = 'http://............/member/login'# 登录的参数params = {"mobile_phone": 18310443005,"pwd": 12345678}headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.post(url=url, json=params, headers=headers)res = response.json()token = res['data']['token_info']['token']print(token)
3、通过jsonpath提取
from jsonpath import jsonpathimport requestsurl = 'http://............/member/login'# 登录的参数params = {"mobile_phone": 18310443005,"pwd": 12345678}headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.post(url=url, json=params, headers=headers)res = response.json()token = jsonpath(res,'$..token')print(token)token_type = jsonpath(res,'$..token_type')print(token_type)
4、jsonpath的语法
$ 根节点:最外层是根节点. or [] 取子节点:下一个节点.. 子孙节点:除根节点外所有节点[] 如数组下标,根据内容选值等如果找不到返回False
七、json模块
** json.load:将json文件读取到python中**
import jsonfrom jsonpath import jsonpath# json文件读取到python中with open('ceshi.json','r',encoding='utf-8')as f:res = json.load(f)# 类型为dictprint(type(res))print(res)# 使用jsonpath获取字典中reg_name数据res = jsonpath(res,'$..data[reg_name]')print(res)
json.loads:将json字符串转换成python数据的字典(与response.json()效果一致)
##### json.loads:将json字符串转换成python数据的字典(与response.json()效果一致)```pythonimport requestsimport jsonurl = 'http://..................../login'# 登录的参数params = {"mobile_phone": 1831044300,"pwd": 12345678}headers = {'X-Lemonban-Media-Type': 'lemonban.v2'}response = requests.post(url=url, json=params, headers=headers)res = response.textprint(type(res),res)res = json.loads(res)print(type(res),res)print()res = response.json()print(type(res),res)
参考链接:day19-- python 处理 http 接口请求 -- 节选自 py31 期某位学员的笔记 - 测试派