import re
import pandas
import pandas as pd
import numpy as np
def get_coordinates(data: pandas.DataFrame, target: str, order_id):
"""
根据要查找的目标,返回其在excel中的位置
data: excel数据,
target: 要查找的目标
return: 返回坐标列表
"""
data_list = np.array(data).tolist()
for i in range(len(data_list)):
for j in range(len(data_list[i])):
if data_list[i][j] == target:
ints=([i + 1, j + 1])
print(ints)
get_x_y(ints,order_id)
return [i + 1, j + 1]
return []
def get_x_y(ints,order_id):
print(ints[0],ints[1] + 2)
from openpyxl import load_workbook
wb = load_workbook('.\weid2.xlsx')
# 激活excel表
sheet = wb.active
# 向excel中写入表头
sheet['j1'] = '订单号'
# sheet['b1'] = '性别'
# sheet['c1'] = '年龄'
# 向excel中写入对应的value
sheet.cell(row=ints[0]+1, column=ints[1] + 3).value = str(order_id)
# sheet.cell(row=2, column=2).value = '男'
# sheet.cell(row=2, column=3).value = 20
wb.save('.\weid2.xlsx')
print('数据写入成功!')
if __name__ == '__main__':
# 读取excel文件
df = pd.read_excel('.\weid2.xlsx')
# df = pd.read_excel(path, header=0)
# for url in df['链接']:
# print("ur", url)
url = 'https://weidian.com/item.html?itemID=6095611655&spider_token=4572'
item_Id = re.findall(r'itemID=(.*?)&spider', url)[0]
itemID = '6095611655'
order_id = '821018030917163' # 订单号
if itemID == item_Id:
print("item_Id", item_Id)
# 以查找张思德为例,获取其所在行和列
target_string = url
# 得到坐标
coordinates = get_coordinates(df, target_string, order_id)
# 打印坐标
print(f"{target_string}在第{coordinates[0]}行,第{coordinates[1]}列") # 张思德在第2行,第3列
![](https://img2023.cnblogs.com/blog/1902253/202304/1902253-20230401003238553-1232559821.png)