最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用Selenium實(shí)現(xiàn)自動(dòng)化網(wǎng)頁批量錄入

 更新時(shí)間:2025年11月25日 09:08:05   作者:小莊-Python辦公  
本文介紹一個(gè)面向上海區(qū)批量入賬的網(wǎng)頁自動(dòng)化小工具的核心腳本,在本地頁面上自動(dòng)點(diǎn)擊新增一行就會(huì)批量填充五列數(shù)據(jù),下面小編就來講講具體的實(shí)現(xiàn)方法吧

前言

本文介紹一個(gè)面向“上海區(qū)批量入賬”的網(wǎng)頁自動(dòng)化小工具的核心腳本 auto_fill.py,如何在本地頁面上自動(dòng)點(diǎn)擊“新增一行”,并批量填充五列數(shù)據(jù)(默認(rèn) 50 行),支持從 CSV 讀取或使用內(nèi)置示例數(shù)據(jù)補(bǔ)齊。

為什么寫這個(gè)工具

  • 背景:上海區(qū)批量入賬場(chǎng)景,網(wǎng)頁僅支持人工逐條錄入,流程耗時(shí)、重復(fù)勞動(dòng)多、易出錯(cuò)。
  • 目標(biāo):通過網(wǎng)頁自動(dòng)化錄入,顯著縮短入賬耗時(shí)、提升準(zhǔn)確率與一致性。
  • 人力與周期:整體人力耗時(shí) 2 個(gè)工作日,其中工具核心功能研發(fā)與聯(lián)調(diào)約 5 小時(shí)。
  • 預(yù)期推廣:通過 1 小時(shí)的集中培訓(xùn)完成安裝與演示,讓財(cái)務(wù)人員可獨(dú)立使用;預(yù)計(jì)獲得“業(yè)務(wù)分獎(jiǎng)勵(lì)/表揚(yáng)信”等認(rèn)可形式。

方案總覽

  • 頁面?zhèn)龋?code>index.html 提供動(dòng)態(tài)表單,每行包含 5 個(gè)輸入框,可點(diǎn)擊 + 新增一行 增加行數(shù),并支持導(dǎo)出多列 CSV。
  • 自動(dòng)化側(cè):auto_fill.py 使用 Selenium 自動(dòng)打開頁面、點(diǎn)擊新增行到目標(biāo)數(shù)量、并逐行填入 5 列數(shù)據(jù)。
  • 數(shù)據(jù)來源:優(yōu)先讀取同目錄 input.csv 的前 50 行×5 列,若不足則按“演示行X-列Y”自動(dòng)補(bǔ)齊。
  • 瀏覽器策略:優(yōu)先連接已開啟的 Chrome 遠(yuǎn)程調(diào)試端口 127.0.0.1:9222,回退到本地 Chrome→Edge→Firefox。

效果圖

核心代碼解析

數(shù)據(jù)準(zhǔn)備:get_values

位置:d:\I_LOVE_KUNKUN\11月\演示案例\auto_fill.py:14

作用:讀取 CSV 的前 rows_count 行(每行最多 5 列),不足的列與行用“演示行X-列Y”補(bǔ)齊,確保最終是 rows_count×5 的二維數(shù)據(jù)。

關(guān)鍵邏輯:

  • 讀取 csv_pathauto_fill.py:12)的內(nèi)容并截取到指定行數(shù)(auto_fill.py:17auto_fill.py:24)。
  • 若行數(shù)不足則補(bǔ)齊(auto_fill.py:27auto_fill.py:33)。

瀏覽器驅(qū)動(dòng):create_driver

  • 位置:d:\I_LOVE_KUNKUN\11月\演示案例\auto_fill.py:36
  • 作用:優(yōu)先使用已啟動(dòng)的 Chrome 遠(yuǎn)程調(diào)試端口連接;失敗時(shí)回退到 Chrome/Edge/Firefox。
  • 關(guān)鍵邏輯:設(shè)置 Options().add_experimental_option('debuggerAddress', '127.0.0.1:9222')auto_fill.py:38auto_fill.py:40)。

自動(dòng)化流程:run

位置:d:\I_LOVE_KUNKUN\11月\演示案例\auto_fill.py:50

步驟:

  • 打開本地頁面 index.htmlauto_fill.py:52)。
  • 等待并獲取“新增一行”按鈕(auto_fill.py:54)。
  • 連續(xù)點(diǎn)擊到目標(biāo)行數(shù)(默認(rèn) count=50,auto_fill.py:55auto_fill.py:58)。
  • 等待行渲染完成并拿到所有行節(jié)點(diǎn)(auto_fill.py:59auto_fill.py:61)。
  • 逐行填入五列數(shù)據(jù)(auto_fill.py:62auto_fill.py:66)。

使用步驟

打開網(wǎng)頁:雙擊 index.html。

運(yùn)行自動(dòng)錄入:在當(dāng)前目錄執(zhí)行:python auto_fill.py

可選(遠(yuǎn)程調(diào)試更穩(wěn)定):先啟動(dòng) Chrome 并開啟端口,再運(yùn)行腳本:

  • Windows:"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" --remote-debugging-port=9222
  • 然后執(zhí)行:python auto_fill.py

完整代碼

from pathlib import Path
import os
import csv
import time
import subprocess
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

base = Path(__file__).resolve().parent
html_path = base / 'index.html'
csv_path = base / 'input.csv'

def find_chrome():
    candidates = [
        r"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
        r"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
        str(Path(os.getenv('LOCALAPPDATA', '')) / 'Google/Chrome/Application/chrome.exe'),
    ]
    for p in candidates:
        if p and Path(p).is_file():
            return p
    return None

def start_chrome_debug(port=9222, user_data_dir=None):
    exe = find_chrome()
    if not exe:
        return False
    if not user_data_dir:
        user_data_dir = str(base / 'chrome_profile')
    os.makedirs(user_data_dir, exist_ok=True)
    cmd = [exe, f'--remote-debugging-port={port}', f'--user-data-dir={user_data_dir}']
    try:
        subprocess.Popen(cmd)
        time.sleep(1.5)
        return True
    except Exception:
        return False

def get_values(rows_count, cols=5):
    vals = []
    try:
        with open(csv_path, 'r', encoding='utf-8-sig') as f:
            for row in csv.reader(f):
                r = []
                for j in range(cols):
                    r.append(row[j] if j < len(row) else '')
                vals.append(r)
                if len(vals) >= rows_count:
                    break
    except Exception:
        vals = []
    if len(vals) < rows_count:
        for i in range(len(vals) + 1, rows_count + 1):
            vals.append([f'演示行{i}-列{j}' for j in range(1, cols + 1)])
    for i in range(rows_count):
        for j in range(cols):
            if vals[i][j] == '':
                vals[i][j] = f'演示行{i + 1}-列{j + 1}'
    return vals

def create_driver():
    try:
        opts = Options()
        opts.add_experimental_option('debuggerAddress', '127.0.0.1:9222')
        return webdriver.Chrome(options=opts)
    except Exception:
        try:
            return webdriver.Chrome()
        except Exception:
            try:
                return webdriver.Edge()
            except Exception:
                return webdriver.Firefox()

def run():
    start_chrome_debug(9222, str(base / 'chrome_profile'))
    driver = create_driver()
    driver.get(html_path.as_uri())
    wait = WebDriverWait(driver, 10)
    add_btn = wait.until(EC.element_to_be_clickable((By.ID, 'addRowBtn')))
    count = 50
    for _ in range(count - 1):
        add_btn.click()
        time.sleep(0.02)
    wait.until(lambda d: len(d.find_elements(By.CSS_SELECTOR, '#rows .row')) >= count)
    rows = driver.find_elements(By.CSS_SELECTOR, '#rows .row')
    values = get_values(count, 5)
    for i in range(count):
        inputs = rows[i].find_elements(By.CSS_SELECTOR, '.value-input')
        for j in range(5):
            inputs[j].clear()
            inputs[j].send_keys(values[i][j])
    time.sleep(2)
    driver.quit()

if __name__ == '__main__':
    run()

CSV 數(shù)據(jù)規(guī)范

文件名:input.csv(與腳本同目錄)。

列數(shù):前 5 列有效;若不足 5 列則空位自動(dòng)補(bǔ)齊為“演示行X-列Y”。

示例:

姓名,電話,地址,公司,備注
張三,13800001234,上海浦東新區(qū),某公司,備注A
李四,13800004321,上海閔行區(qū),某公司B,備注B

常見問題與排查

無法啟動(dòng)瀏覽器/驅(qū)動(dòng)報(bào)錯(cuò):

  • 確認(rèn)已安裝 Chrome/Edge/Firefox 任一瀏覽器。
  • 若使用遠(yuǎn)程調(diào)試端口,確保端口 9222 已被 Chrome 打開。

頁面未渲染到 50 行:

  • 適當(dāng)增大點(diǎn)擊間隔(auto_fill.py:58time.sleep(0.02))。
  • 檢查頁面按鈕 id 與行選擇器是否與 index.html 保持一致。

input.csv 不存在或空:腳本會(huì)自動(dòng)用“演示行X-列Y”補(bǔ)齊,不影響演示。

擴(kuò)展方向

  • 讀取 Excel(xlsx):可采用 openpyxlpandas 讀取為二維數(shù)據(jù)后復(fù)用填充邏輯。
  • 表單字段校驗(yàn)與模板:在 index.html 增加列名映射與必填校驗(yàn)提示。
  • 更大批量與穩(wěn)態(tài)優(yōu)化:分批點(diǎn)擊與分批填充,適當(dāng)增加等待以提高穩(wěn)定性。
  • 日志與異常處理:加入詳細(xì)日志、截圖與失敗重試,便于問題定位。

結(jié)語

該工具圍繞“批量入賬”的剛性需求,利用 Selenium 將重復(fù)的人工錄入流程自動(dòng)化。在 2 個(gè)工作日的整體投入與約 5 小時(shí)的核心研發(fā)下,達(dá)成了可培訓(xùn)、可推廣、可復(fù)用的自動(dòng)化方案,有望在上海區(qū)獲得“業(yè)務(wù)分獎(jiǎng)勵(lì)/表揚(yáng)信”的認(rèn)可,并為后續(xù)流程持續(xù)優(yōu)化奠定基礎(chǔ)。

以上就是Python使用Selenium實(shí)現(xiàn)自動(dòng)化網(wǎng)頁批量錄入的詳細(xì)內(nèi)容,更多關(guān)于Python Selenium網(wǎng)頁錄入的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

梓潼县| 工布江达县| 开江县| 洛南县| 镇坪县| 呼伦贝尔市| 虞城县| 会同县| 南漳县| 芦溪县| 盐源县| 西乡县| 三穗县| 乐至县| 华坪县| 龙江县| 新昌县| 阜宁县| 清新县| 永川市| 衢州市| 北流市| 南陵县| 刚察县| 忻城县| 新平| 灵台县| 潜江市| 历史| 射洪县| 桂林市| 敦化市| 大荔县| 兖州市| 武定县| 清徐县| 土默特左旗| 德兴市| 息烽县| 修武县| 犍为县|