用python写个半自动化数据工具,2-3天活 10分钟干完
没有功能?创造功能也得上!
上面是工具的完整演示视频,下面具体分享一下工具的前期立项和在工具制作中遇到的实际问题。分享这个的目的1是用作后面个人的复习了解;目的2就是一个分享。
前期立项:
年末了,领导想对云上的暴露面做个了解,然后针对存在隐患的做一下提醒和整改。但是云平台的SLB负载均衡页面没有提供下载功能,也没有提供API接口,导致想要将数据弄下来,只能手动输入或者鼠标复制粘贴,当中的过程过于复杂,基本上数据全部弄完整理好,就要2-3天,过程还煎熬,所以就考虑能不能将过程自动化掉【前期的想法就是全自动化,而不是视频中展现的半自动化】,就有上面的工具的诞生。
1.确定需要提取的数据内容
通过右键【检查】获取网页元素信息可以看到 想要的单条数据 是在 next-table-cell-wrapper
里面
但是只要这么一块是不够的 我需要这一条所有的信息块 那就是在元素 next-table-cell-wrapper
中
那这里python就好干了 用循环去便利去替换后面的序号,只要是的就存入xlsx表格中,检测到没有了,就退出循环。代码基本如下:
def Data_processing_and_storage():
try:
while True: # 内层循环:提取当前页面的数据
if num > 0:
print(f"处理数据次数:{num}")
elements = wait.until(EC.presence_of_all_elements_located(
(By.XPATH, f"//div[contains(@class, 'next-table-cell-wrapper') and @data-next-table-row='{row_number}']")))
ws.cell(row=excel_row, column=1).value = value
for col, element in enumerate(elements, start=2): # 列号从2开始
ws.cell(row=excel_row, column=col).value = element.text #插入SLB实例ID信息
excel_row += 1
rows_added += 1
row_number += 1
num += 1
wb.save(data_name)
except Exception as e:
print(f"提取当前页面数据完毕: {value}, {e}")
finally:
# 保存工作簿
wb.save(data_name)
return rows_added
这样即可将数据提取出来,但是这还只是一个页面,那其他页面咋办,如果跳转到其他页面呢?
2.提取不同SLB页面信息
经过我的发现,在URL中就已经给出了答案
URL中是通过变化不同的SLB实例ID信息来打开不同的页面,那我可以将所有的SLB信息先弄出来放到表格里,然后让python去遍历,依次替换到URL中并访问即可实现网页的切换。代码如下:
def Enterweb(value, driver):
url = f'https://***/ascm/slb/caseManagement/caseManagementDetail/{value}/listeners/productName=slbnext'
driver.get(url)
def load_ids(file_name):
try:
wb_ids = load_workbook(file_name)
ws_ids = wb_ids.active
data = [(row[0].value, row[1].value) for row in ws_ids.iter_rows(min_row=1)]
return data
except FileNotFoundError:
print(f"文件 {file_name} 未找到。")
return []
except Exception as e:
print(f"读取文件时发生错误: {e}")
return []
3.翻页问题
有的SLB开放端口过多,这将导致一个页面放不下,那得考虑如果让其翻页处理,还是如刚刚的数据查询一样,查看翻页按钮的元素信息是什么,Python去遍历,并点击。
代码如下:
def click_next_page(wait):
try:
next_page_btn = wait.until(EC.element_to_be_clickable(
(By.XPATH, "//i[contains(@class, 'next-icon-arrow-right') and contains(@class, 'next-pagination-icon-next')]")))
next_page_btn.click()
print("已经跳转到下一页。")
except Exception as e:
print(f"跳转下一页时出现错误: {e}")
4.其他问题处理
有了这几个大项,只需要在完成一些小的功能即可初步运行,比如文件存放,浏览器的调用,tk界面的布置,如果要完全自动化,就需要加入页面翻页的逻辑。代码如下:
# 检查或创建Excel文件
def check_or_create_excel(data_name):
if Path(data_name).is_file():
workbook = load_workbook(filename=data_name)
else:
workbook = Workbook()
workbook.save(filename=data_name)
return workbook
#链接当前已打开Google浏览器的9222端口进行数据交互
def configure_browser():
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9222")
service = Service('C:\\Users\\Administrator\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe')
driver = webdriver.Chrome(service=service, options=chrome_options)
wait = WebDriverWait(driver, 7)
return driver,wait
def TKK():
global root
root = tk.Tk()
root.title("数据输入半自动化工具(快速版)")
# 创建一个用于固定窗口在最前端的按钮
topmost_button = tk.Button(root, text="固定窗口", command=lambda: toggle_topmost(topmost_button))
topmost_button.pack(pady=5)
# 设置窗口大小
root.geometry("500x500") # width x height
# 创建Text控件用于显示输出
output_box = tk.Text(root, height=15, width=70)
output_box.pack(pady=10)
# 定义文本样式
output_box.tag_configure('big_red', foreground='red', font=('Helvetica', '13', 'bold'))
#output_box.config(state=tk.DISABLED) # 初始时禁止编辑
output_box.insert(tk.END, "先选【云区】,在点击【切换下一个SLB】按键开始程序",'big_red')
#print("程序启动后 请先点击【切换下一个SLB】开始程序")
# 重定向print到Text控件
sys.stdout = TextRedirector(output_box)
# 创建按钮
button_y = tk.Button(root, text='录入当前页面数据', command=lambda: on_click_y())
button_y.pack(pady=5)
button_n = tk.Button(root, text='切换下一个SLB', command=on_click_n)
button_n.pack(pady=5)
button_x = tk.Button(root, text=' 下一页 ', command=on_click_x)
button_x.pack(pady=5)
button_o = tk.Button(root, text='退出程序', command=on_click_o,fg='black', bg='red')
button_o.pack(pady=5)
root.protocol("WM_DELETE_WINDOW", on_click_o)
# 创建容器(Frame)放在底部,用于放置按钮
bottom_frame = tk.Frame(root)
bottom_frame.pack(side=tk.BOTTOM, fill=tk.X, padx=10, pady=10)
button_x = tk.Button(bottom_frame, text=' 公有云 ', command=on_click_gong)
button_x.pack(side=tk.LEFT,padx=50)
button_x = tk.Button(bottom_frame, text=' 专有云 ', command=on_click_zhuan)
button_x.pack(side=tk.LEFT,padx=40)
button_x = tk.Button(bottom_frame, text=' 信创云 ', command=on_click_xin)
button_x.pack(side=tk.LEFT,padx=50)
#tk程序主循环
root.mainloop()
#程序主逻辑
def main():
global driver, wait, instance_ids, current_instance_index,temporary_value
driver, wait = configure_browser() # 在这里调用configure_browser
instance_ids = load_instance_ids() # 加载实例ID
current_instance_index = 0 # 初始化实例ID索引
TKK()
if __name__ == "__main__":
main()
最后就由自动化,全部重构成了 半自动化,虽然与自动化相比多了很多点击的步骤,但是效率比以前是高了好几个数量级。【上面的代码,各位拿去直接运行不了的,有些部分简化了,因为这个得根据实际情况去变更代码和逻辑,如果真想了解,我会将工具上传到博客中,但是环境的适配你们可能也得花不少功夫】