python requests 下载excel_python+request+excel

本文介绍如何使用Python的requests库下载Excel文件,并通过xlrd和xlutils库进行读写操作。创建HandleExcel类来处理Excel数据,包括读取单元格值、写入单元格值等功能。此外,还展示了如何运行Excel中的测试用例,调用RunMain类的run_main方法执行HTTP请求,并根据预期结果判断测试是否通过。
摘要由CSDN通过智能技术生成

1、处理一个excel基本方法

单独写一个handle_excel.py

from xlutils.copy import copy

import xlrd

class HandleExcel:

def __init__(self,file='D:\\Users\\wqa\\Desktop\\demo.xls',sheet_id=0):

self.file = file

self.sheet_id = sheet_id

self.data = self.get_data()

def get_data(self):

data = xlrd.open_workbook(self.file)

sheet = data.sheet_by_index(self.sheet_id)

return sheet

def get_rows(self):

rows=self.data.nrows

return rows

def get_value(self, row, col):

value = self.data.cell_value(row, col)

return value

def write_value(self, row, col, value):

data = xlrd.open_workbook(self.file)

data_copy = copy(data)

sheet = data_copy.get_sheet(0) # 取得复制文件的sheet对象

sheet.write(row, col, value) # 在某一单元格写入value

data_copy.save(self.file)

# 封装excel的列名常量

def get_no():

no = 0

return no

def get_apitype():

apitype = 1

return apitype

def get_url():

url = 2

return url

def get_method():

"""获取method"""

method = 3

return method

def get_header():

"""获取header"""

header = 4

return header

def get_purpose():

purpose = 5

return purpose

def get_params():

"""获取params"""

params = 6

return params

def get_expectvalue():

"""获取expectValue"""

expectvalue = 7

return expectvalue

def get_resultValue():

"""获取resultValue"""

resultValue = 8

return resultValue

2、 跑excel中用例

建一个run_handle_excel.py

from customerivr.run_method import RunMain

from customerivr.handle_excel import *

import jsonpath

import json

class RunTestCase:

def __init__(self):

self.Runmain = RunMain()

self.data = HandleExcel()

def go_run(self):

rows_count = self.data.get_rows()

for i in range(1, 2):

url = self.data.get_value(i, get_url())

method = self.data.get_value(i, get_method())

data = self.data.get_value(i, get_params())

expect = self.data.get_value(i, get_expectvalue())

res = self.Runmain.run_main(url, method, data) # 调用get/post主函数

txt = jsonpath.jsonpath(res, '$..text')

stxt = " ".join(txt)

print(stxt)

self.data.write_value(i, get_resultValue(), txt)

if expect in stxt:

print('测试通过')

self.data.write_value(i, get_resultValue(), txt)

else:

print('测试失败')

self.data.write_value(i, get_resultValue(), "测试不通过")

if __name__ == '__main__':

run = RunTestCase()

run.go_run()

# run = RunMain()

# print(run.run_main(url, 'POST', data))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值