python读写excel内容作为参数进行接口请求

# -- coding: utf-8 --**
import openpyxl
from openpyxl.styles import PatternFill
from selenium.webdriver.support.wait import WebDriverWait
import os
import time
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
# 获取当前目录
current_directory = os.getcwd()
import requests
from urllib.parse import quote
# 获取当前目录下的所有文件名
file_names = [filename for filename in os.listdir(current_directory) if filename.endswith('.xlsx')]
print(file_names)
print("Excel文件列表:")
interface_content=[]
excel_content=[]
for filename in file_names:

# 打开Excel文件
workbook = openpyxl.load_workbook(filename) # 替换为你的Excel文件路径

# 选择工作表
sheet = workbook.active # 选择活动工作表,或者使用 workbook["工作表名称"] 选择特定工作表

# 指定要读取的列号(A列为1,B列为2,依此类推)
column_number2 = 2 # 读取B列的数据
column_number1 = 1
# 遍历指定列的所有行
column_data1 = []
column_data2 = []
for row in sheet.iter_rows(min_col=column_number2, max_col=column_number2, values_only=True):
cell_value = row[0]
if cell_value is not None:
column_data2.append(cell_value)
for row in sheet.iter_rows(min_col=column_number1, max_col=column_number1, values_only=True):
cell_value = row[0]
if cell_value is not None:
column_data1.append(cell_value)

# 输出获取的列数据
# for value in column_data2:
# print(value)
for value in column_data1:
print(value)
# 关闭Excel文件
workbook.close()
import time

start_time = time.time()
for i in range(len(column_data1)):
excel_content.append(column_data1[i])
chinese_text = str(column_data1[i])
encoded_text = quote(chinese_text)
print("Encoded Text:", encoded_text)

# 要测试的接口URL
url = 'http://42.159.193.133:8886//question/'+encoded_text

# 发送GET请求
response = requests.get(url)

# 检查响应状态码
if response.status_code == 200:
data = response.json()
print("Response Data:")
print(data['response'])

else:
print(f"Request Failed with status code: {response.status_code}")

end_time = time.time()
elapsed_time = end_time - start_time
print("当前运行了"+str(i+1)+"个接口")
print(f"程序运行时间:{elapsed_time:.6f} 秒")

你可能感兴趣的:(python,excel,开发语言)