import os import time import psutil from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium.webdriver.common.by import By import openpyxl

检查是否已经存在浏览器实例,如果存在则不再创建

for proc in psutil.process_iter(): try: if 'chrome' in proc.name() and '--remote-debugging-port=9224' in proc.cmdline(): options = Options() options.add_experimental_option('debuggerAddress', '127.0.0.1:9224') break except: pass else: # 创建浏览器实例 os.system(r'start chrome --remote-debugging-port=9224 --user-data-dir='D:\评阅用'') options = Options() options.add_experimental_option('debuggerAddress', '127.0.0.1:9224')

在已有的浏览器实例中查找标签页

driver = webdriver.Chrome(options=options) tabs = driver.window_handles for tab in tabs: driver.switch_to.window(tab) if driver.title == '考后核验': print('登陆成功') break

打开Excel表格

wb = openpyxl.load_workbook(r'C:\Users\Administrator\Desktop\考后核验.xlsx') sheet = wb.active

创建照片文件夹

if not os.path.exists(r'C:\Users\Administrator\Desktop\照片'): os.mkdir(r'C:\Users\Administrator\Desktop\照片')

遍历时间单元K列

for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=11, max_col=11): for cell in row: # 获取时间单元内的数据 data = cell.value if data: # 创建子文件夹 folder_name = time.strftime('%Y-%m-%d %H-%M-%S', time.strptime(data, '%Y-%m-%d %H:%M:%S')) folder_path = os.path.join(r'C:\Users\Administrator\Desktop\照片', folder_name) if not os.path.exists(folder_path): os.mkdir(folder_path)

遍历表格内'A'列数据

for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=1, max_col=1): for cell in row: # 获取学号 student_id = cell.value if student_id: # 在网页内查找考生信息 search_input = driver.find_element(By.XPATH, '//input[@placeholder='姓名/考生号']') search_input.clear() search_input.send_keys(student_id) time.sleep(1) # 等待页面加载完成 search_button = driver.find_element(By.XPATH, '//button[@class='ant-btn ant-btn-primary']') driver.execute_script('arguments[0].click();', search_button) time.sleep(1) # 等待页面加载完成

        # 在网页内查找时间和考场编号
        time_elements = driver.find_elements(By.XPATH, '//td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and @style='text-align: center;']')
        room_elements = driver.find_elements(By.XPATH, '//td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and @style='text-align: center;']')
        
        for time_element, room_element in zip(time_elements, room_elements):
            time_text = time_element.text
            room_text = room_element.text
            
            # 比较时间和考场编号与表格内数据是否匹配
            for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=11, max_col=11):
                for cell in row:
                    data = cell.value
                    if data and time_text == data:
                        for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, min_col=8, max_col=8):
                            for cell in row:
                                room_data = cell.value
                                if room_data and room_text == room_data:
                                    # 创建考场编号文件夹
                                    room_folder_path = os.path.join(folder_path, room_text)
                                    if not os.path.exists(room_folder_path):
                                        os.mkdir(room_folder_path)
                                    
                                    # 获取学号和姓名
                                    student_id_element = driver.find_element(By.XPATH, '//td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and text()='{}']/following-sibling::td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and position()=2]'.format(student_id))
                                    student_id_text = student_id_element.text
                                    name_element = driver.find_element(By.XPATH, '//td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and text()='{}']/following-sibling::td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and position()=3]'.format(student_id))
                                    name_text = name_element.text
                                    
                                    # 下载照片
                                    photo_element = driver.find_element(By.XPATH, '//td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and text()='{}']/following-sibling::td[@class='ant-table-column-has-actions ant-table-column-has-sorters' and position()=4]/img'.format(student_id))
                                    photo_src = photo_element.get_attribute('src')
                                    photo_name = '{}_{}.jpg'.format(student_id_text, name_text)
                                    photo_path = os.path.join(room_folder_path, photo_name)
                                    driver.get(photo_src)
                                    with open(photo_path, 'wb') as f:
                                        f.write(driver.page_source)
                                    break
                            else:
                                continue
                            break
                        else:
                            continue
                        break
                    else:
                        continue
                    break
                else:
                    continue
                break
            else:
                continue
            break

关闭浏览器

driver.quit()

考后核验照片下载工具

原文地址: https://www.cveoy.top/t/topic/fIYx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录