import os
import time
import psutil
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import openpyxl
from urllib import request

# Excel表格路径
EXCEL_FILE = r'C:\Users\Administrator\Desktop\考后核验.xlsx'
# 照片保存路径
PHOTO_FOLDER = r'C:\Users\Administrator\Desktop\照片'

# 检查是否已经存在浏览器实例,如果存在则不再创建
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(EXCEL_FILE)
sheet = wb.active

# 创建照片文件夹
if not os.path.exists(PHOTO_FOLDER):
    os.mkdir(PHOTO_FOLDER)

# 遍历时间单元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(PHOTO_FOLDER, 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 = WebDriverWait(driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//input[@placeholder='姓名/考生号']'))
            )
            search_input.clear()
            search_input.send_keys(student_id)
            search_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, '//button[@class='ant-btn ant-btn-primary']'))
            )
            search_button.click()

            # 等待页面加载完成
            time.sleep(2)

            # 遍历表格内'K'列数据,查找对应时间的照片
            for row_time in sheet.iter_rows(min_row=row[0].row, max_row=row[0].row, min_col=11, max_col=11):
                for cell_time in row_time:
                    exam_time = cell_time.value
                    if exam_time:
                        # 在网页内查找对应时间的照片元素
                        time_elements = WebDriverWait(driver, 10).until(
                            EC.presence_of_all_elements_located((By.XPATH, '//td[contains(text(), '' + exam_time + '')]'))
                        )
                        img_elements = driver.find_elements(By.XPATH, '//div[@class='img-lazy-load']/img')

                        # 下载照片
                        for time_element, img_element in zip(time_elements, img_elements):
                            folder_name = time.strftime('%Y-%m-%d %H-%M-%S', time.strptime(exam_time, '%Y-%m-%d %H:%M:%S'))
                            folder_path = os.path.join(PHOTO_FOLDER, folder_name)
                            img_url = img_element.get_attribute('src')
                            img_name = f'{student_id}.jpg'
                            img_path = os.path.join(folder_path, img_name)
                            request.urlretrieve(img_url, img_path)

driver.quit()
Python+Selenium自动化考后核验及照片分类

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

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