admin 管理员组

文章数量: 1087652

如何用python请求接口

作用:用来重复插入数据

1、首先下载好python 然后 cmd 输入 python 判断是否全局安装成功

2、下载好PcIdea 

3、下载好自己需要的库

4、查看请求网络

         1. 按好F12

         2. 对接口进行请求

         3. 先将需要的header请求头标数据进行验证

         4. 查看正文请求

5、给你一个模板自己慢慢体会 


import requests                         #导入requests包
import random
import json
import time# response = requests.get('')
# print(response.status_code)  # 打印状态码
# print(response.url)          # 打印请求url
# print(response.headers)      # 打印头信息
# print(response.cookies)      # 打印cookie信息
# print(response.text)  #以文本形式打印网页源码
# print(response.content) #以字节流形式打印
# print(response.json())AddCarURL = "http://localhost:10086/jeecg-boot/cable/insurance/add"
AddCarCookies = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjkwOTUyNzcsInVzZXJuYW1lIjoiYWRtaW4ifQ.Kn1jB5gUkCdnszSVxnjVVKtRMbx_WJSVZW6G-yJaid8"
randomStr = "ABCDEFGHIJKLMNOPQRST"
randomInsurance = ["泰康人寿","阳光保险","新华保险","太平人寿","同方全球人寿","友邦保险"," 招商仁和","平安保险"]
randomBox = ["石鲜仓储","明顺仓储","雄星仓储","金盛仓储","旺恒仓储","中豹仓储","飞翔仓储","速发仓储","春天仓储"]
sess = requests.session()
headers = {"X-Access-Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjkxMDUyMjUsInVzZXJuYW1lIjoiYWRtaW4ifQ.EUAjJYACel8QHFw4AFERIaDjXZJTwOOyp8uncqx3Jps","User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19041","Referer":"http://localhost:3000/cable/VehicleList","Origin":"http://localhost:3000","Host":"localhost:10086","Content-Type":"application/json; charset=utf-8"
}a1=(2017,1,1,0,0,0,0,0,0)              #设置开始日期时间元组(1976-01-01 00:00:00)
a2=(2019,3,16,23,59,59,0,0,0)    #设置结束日期时间元组(1990-12-31 23:59:59)a3=(2019,3,16,0,0,0,0,0,0)              #设置开始日期时间元组(1976-01-01 00:00:00)
a4=(2021,8,16,23,59,59,0,0,0)    #设置结束日期时间元组(1990-12-31 23:59:59)start=time.mktime(a1)    #生成开始时间戳
end=time.mktime(a2)      #生成结束时间戳start1=time.mktime(a3)    #生成开始时间戳
end2=time.mktime(a4)      #生成结束时间戳# for i in range(10):
#     t=random.randint(start,end)    #在开始和结束时间戳中随机取出一个
#     date_touple=time.localtime(t)          #将时间戳生成时间元组
#     date=time.strftime("%Y-%m-%d",date_touple)  #将时间元组转成格式化字符串(1976-05-21)
#
#     t1 = random.randint(start1,end2)
#     date_touplen=time.localtime(t1)          #将时间戳生成时间元组
#     dateOne=time.strftime("%Y-%m-%d",date_touplen)  #将时间元组转成格式化字符串(1976-05-21)
#     print(date + " " +dateOne)def addCar(num:int):# {"type": "2", "carryingCapacity": "3吨", "license": "粤A888", "engineNumber": "123456", "state": 0}# {"vehicleId": "2", "insuraName": "人寿保险", "insurancePolicy": "UY1254SD5492W", "insuranceDateBegin": "2021-08-16",#  "insuranceDateEnd": "2021-08-31", "strongPolicy": "UY1254SD5492E", "strongDateBegin": "2021-07-01",#  "strongDateEnd": "2021-08-28", "license": "2"}for i in range(num):t = random.randint(start, end)  # 在开始和结束时间戳中随机取出一个date_touple = time.localtime(t)  # 将时间戳生成时间元组date = time.strftime("%Y-%m-%d", date_touple)  # 将时间元组转成格式化字符串(1976-05-21)t1 = random.randint(start1, end2)date_touplen = time.localtime(t1)  # 将时间戳生成时间元组dateOne = time.strftime("%Y-%m-%d", date_touplen)  # 将时间元组转成格式化字符串(1976-05-21)datas = {}datas["vehicleId"] = "2"datas["insuraName"] = random.choice(randomInsurance)datas["insurancePolicy"] = random.choice(randomStr) + str(random.randint(10000, 99999)) + random.choice(randomStr) + str(random.randint(10000, 99999));datas["insuranceDateBegin"] = datedatas["insuranceDateEnd"] = dateOnedatas["strongPolicy"] = random.choice(randomStr) + str(random.randint(10000, 99999)) + random.choice(randomStr) + str(random.randint(10000, 99999));datas["strongDateBegin"] = datedatas["strongDateEnd"] = dateOnedatas["license"] = iprint(datas)res=requests.post(url=AddCarURL, data=json.dumps(datas), headers=headers)print(res.text)addCar(100)

记得要通过 token 验证,还有根据数据转json 即可

本文标签: 如何用python请求接口