python接口自动化—request模块

01 request模块基本操作

# 导入requests模块
import requests

# ===准备请求相关数据===

url = "https://api.tuhu.cn/Product/GetTireList"

headers = {
    "content-type": "application/json"
}

data = {"orderType": 0, "vehicleId": "VE-GM-S07BT", "onlyOe": False, "province": "上海市", "provinceId": "", "city": "上海市",
        "cityId": "", "pageIndex": 1,
        "filter": {"BrandName": "", "TireSize": "195/55R15", "TireTab": "", "TireSeason": "", "MaxPrice": "",
                   "MinPrice": "", "TireRof": "防爆,非防爆", "TireSpeedRating": "", "TirePattern": "",
                   "SpecialTireSize": ""}}

# ===发送请求===
response = requests.post(url=url, json=data, headers=headers)

发送请求

------POST--------

1、参数类型为:"Content-Type": "application/json",发送请求的时候,使用json来传递参数
response = requests.post(url=url,json=data)
2、参数类型为:Content-Type": "application/x-www-form-urlencoded,使用data来进行传递
response = requests.post(url=url,data=data)

3、文件上传接口的参数类型为:"Content-Type": "multipart/form-data;文件应该使用files这个参数来进行传递

 file = {"pic": ("bj01.png", open(r"C:UsersMuSenDesktoppageimageico.png", "rb"), "image/png")}

response = requests.post(url=url, files=file,data=data)

------GET-------

1、处理方式一:放到url地址后面
url = "http://httpbin.org/get?name=musen&age=18"

2、处理方式二:使用params来进行传递
url1 = "http://httpbin.org/get"
data = {
"name":"musen",
"age":18
}
response = requests.get(url=url1,params=data)
print(response.text)

打印返回内容
1、text属性:自动识别内容的编码方式,有可能识别不准确出现乱码
res1 = response.text
print(res1,type(res1)) ---string 类型


2、json方法:将字符串中的json类型数据转换为对应的python值
使用json方法的前提:返回数据必须是json格式的
res3 = response.json()
print(res3, type(res3))  ----dict类型

3、content属性
res5 = response.content   #返回字节形式的数据
res5 = response.content.decode("utf8")  ---string 类型   #指定utf-8格式进行编码,手动自动编码方式,对页面内容进行解码
print(res5, type(res5))

 

提取返回结果中的参数

import jsonpath

res = {'code': 0,
       'msg': 'OK',
       'data': {'id': 7800007,
                'leave_amount': 0.0,
                'mobile_phone': '18189098765',
                'reg_name': '小柠檬',
                'reg_time': '2020-03-21 09:49:27.0',
                'type': 1,
                'token_info': {'token_type': 'Bearer',
                               'expires_in': '2020-03-21 11:16:59',
                               'token': 'eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A'}},
       'data1': {'id': 7800007,
                 'leave_amount': 0.0,
                 'mobile_phone': '18189098765',
                 'reg_name': '小柠檬',
                 'reg_time': '2020-03-21 09:49:27.0',
                 'type': 1,
                 'token_info': {'token_type': 'Bearer',
                                'expires_in': '2020-03-21 11:16:59',
                                'token': 'eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A'}},
                     }

#  使用字典的键值对 提取方式去提取
# # 提取用户的id
member_id = res["data"]["id"]
print(member_id)
#
# # 提取token值
token = res["data"]["token_info"]["token"]
print(token)


# 使用jsonpath来提取
member_id = jsonpath.jsonpath(res, "$.data..id")[0]
print(member_id,type(member_id))

token = jsonpath.jsonpath(res, "$.data1..token")[0]
print(token)

"""
. 代表直接子节点
.. 代表子孙节点(不管层级)

"""

拓展:

文件下载

res = requests.get(url="http://www.lemonban.com/images/upload/image/20190219/1550554131699.jpg")

print(res.content)

with open("lemon.png","wb") as f:
    f.write(res.content)
原文地址:https://www.cnblogs.com/erchun/p/12546849.html