Unify export-task matching to time tolerance only (≤40s)

去掉顺心/中通/韵达/安能(应到) 导出任务认领时的标题/模块名校验,
统一改为仅按提交时间容差 ≤40 秒匹配本批任务。

理由:单账号无并发,时间窗内的任务必为本批所建;而站点可能调整
任务标题名,写死标题会导致匹配失败、任务已创建却一直查不到。
同步清理因此变成死代码的局部变量与函数形参。

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-07-06 22:04:22 +08:00
parent d05848ee36
commit 63ee8bbae1
4 changed files with 83 additions and 113 deletions

View File

@@ -52,8 +52,6 @@ def set_cdp_port(port):
CDP_PORT = int(port) CDP_PORT = int(port)
# 导出下载页里,本批任务的“导出功能名称”固定为“交接单明细”。
TARGET_EXPORT_FUNC = "交接单明细"
FINAL_FILENAME = "安能-应到货物数据.xlsx" FINAL_FILENAME = "安能-应到货物数据.xlsx"
# 进站交接单查询页 / 导出下载页的 URL 特征(用于在 /json 里定位/复用 tab 目标)。 # 进站交接单查询页 / 导出下载页的 URL 特征(用于在 /json 里定位/复用 tab 目标)。
@@ -651,18 +649,20 @@ def _click_export_query(export_cdp):
def _match_our_tasks(rows, export_times): def _match_our_tasks(rows, export_times):
"""从行里挑出本批任务(导出功能名称=交接单明细 且 导出时间接近某次导出时刻)。""" """从行里挑出本批任务:仅按导出时间容差(40s)匹配,不校验导出功能名称。
单账号无并发,本批提交的任务必为我们创建;而站点可能调整导出功能名称,
写死名称反而会导致匹配失败、任务一直查不到。
"""
matched = [] matched = []
for row in rows: for row in rows:
cell = row.get("cellMap", {}) cell = row.get("cellMap", {})
if cell.get("导出功能名称") != TARGET_EXPORT_FUNC:
continue
time_str = cell.get("导出时间", "") time_str = cell.get("导出时间", "")
try: try:
row_time = datetime.strptime(time_str.strip(), "%Y-%m-%d %H:%M:%S") row_time = datetime.strptime(time_str.strip(), "%Y-%m-%d %H:%M:%S")
except ValueError: except ValueError:
continue continue
if any(abs((row_time - et).total_seconds()) <= 120 for et in export_times): if any(abs((row_time - et).total_seconds()) <= 40 for et in export_times):
row["_time"] = row_time row["_time"] = row_time
matched.append(row) matched.append(row)
return matched return matched
@@ -672,7 +672,7 @@ def poll_and_download_tasks(export_cdp, export_times, download_dir):
"""轮询导出下载页直到本批任务齐全且全部“导出完成”,再逐个免对话框下载。""" """轮询导出下载页直到本批任务齐全且全部“导出完成”,再逐个免对话框下载。"""
total_expected = len(export_times) total_expected = len(export_times)
print(f">> 轮询导出下载任务(期望 {total_expected}{TARGET_EXPORT_FUNC}...") print(f">> 轮询导出下载任务(期望 {total_expected} 个)...")
while True: while True:
wait_until( wait_until(
export_cdp, export_cdp,

View File

@@ -206,22 +206,20 @@ def shunxin_expected_download(page):
pending_tasks = 0 pending_tasks = 0
current_ready_timestamps = [] current_ready_timestamps = []
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
for i in range(row_count): for i in range(row_count):
tds = rows.nth(i).locator("td") tds = rows.nth(i).locator("td")
if tds.count() < 9: if tds.count() < 9:
continue continue
submit_time_str = tds.nth(2).inner_text().strip() submit_time_str = tds.nth(2).inner_text().strip()
title_str = tds.nth(5).inner_text().strip()
status_str = tds.nth(6).inner_text().strip() status_str = tds.nth(6).inner_text().strip()
if title_str == "发车管理运单列表":
try: try:
row_time = datetime.strptime( row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
submit_time_str, "%Y-%m-%d %H:%M:%S"
)
matched = any( matched = any(
abs((row_time - et).total_seconds()) <= 60 abs((row_time - et).total_seconds()) <= 40
for et in export_times for et in export_times
) )
@@ -454,22 +452,20 @@ def shunxin_actual_download(page):
pending_tasks = 0 pending_tasks = 0
current_ready_timestamps = [] current_ready_timestamps = []
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
for i in range(row_count): for i in range(row_count):
tds = rows.nth(i).locator("td") tds = rows.nth(i).locator("td")
if tds.count() < 9: if tds.count() < 9:
continue continue
submit_time_str = tds.nth(2).inner_text().strip() submit_time_str = tds.nth(2).inner_text().strip()
title_str = tds.nth(5).inner_text().strip()
status_str = tds.nth(6).inner_text().strip() status_str = tds.nth(6).inner_text().strip()
if title_str.startswith("卸车扫描记录"):
try: try:
row_time = datetime.strptime( row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
submit_time_str, "%Y-%m-%d %H:%M:%S"
)
matched = any( matched = any(
abs((row_time - et).total_seconds()) <= 60 abs((row_time - et).total_seconds()) <= 40
for et in export_times for et in export_times
) )

View File

@@ -81,7 +81,6 @@ def yunda_smart_menu_click(page, menu_path):
def yunda_expected_download(page): def yunda_expected_download(page):
"""韵达:应到货物数据下载""" """韵达:应到货物数据下载"""
print("\n▶ 开始执行【韵达 - 应到货物数据下载】任务...") print("\n▶ 开始执行【韵达 - 应到货物数据下载】任务...")
target_task_title = "进站主单表"
download_dir = DOWNLOAD_DIR download_dir = DOWNLOAD_DIR
if not os.path.exists(download_dir): if not os.path.exists(download_dir):
@@ -245,9 +244,7 @@ def yunda_expected_download(page):
try: try:
confirm_link.wait_for(state="visible", timeout=6000) confirm_link.wait_for(state="visible", timeout=6000)
if export_frame.get_by_text("导出任务建立成功").is_visible(): if export_frame.get_by_text("导出任务建立成功").is_visible():
print( print(" ✅ 导出任务已建立成功。")
" ✅ 导出任务已建立成功。"
)
confirm_link.click() confirm_link.click()
inner_success = True inner_success = True
break break
@@ -311,7 +308,6 @@ def yunda_expected_download(page):
_yunda_poll_and_download_tasks( _yunda_poll_and_download_tasks(
page, page,
export_times, export_times,
target_task_title,
download_dir, download_dir,
"韵达-应到货物数据.xlsx", "韵达-应到货物数据.xlsx",
) )
@@ -324,7 +320,6 @@ def yunda_expected_download(page):
def yunda_actual_download(page): def yunda_actual_download(page):
"""韵达:实到货物数据下载""" """韵达:实到货物数据下载"""
print("\n▶ 开始执行【韵达 - 实到货物数据下载】任务...") print("\n▶ 开始执行【韵达 - 实到货物数据下载】任务...")
target_task_title = "扫描记录数据"
download_dir = DOWNLOAD_DIR download_dir = DOWNLOAD_DIR
if not os.path.exists(download_dir): if not os.path.exists(download_dir):
@@ -406,17 +401,13 @@ def yunda_actual_download(page):
match_total = re.search(r"总共\s*(\d+)\s*条记录", info_text) match_total = re.search(r"总共\s*(\d+)\s*条记录", info_text)
if match_total and int(match_total.group(1)) > 0: if match_total and int(match_total.group(1)) > 0:
has_records = True has_records = True
print( print(f" ✅ 实到数据已加载,总记录数: [{match_total.group(1)}] 条。")
f" ✅ 实到数据已加载,总记录数: [{match_total.group(1)}] 条。"
)
if not has_records: if not has_records:
if ws_frame.locator( if ws_frame.locator(
".no-records-found", has_text="没有找到匹配的记录" ".no-records-found", has_text="没有找到匹配的记录"
).first.is_visible(): ).first.is_visible():
print( print(" ⚠️ 当前查询范围内为空数据,终止并关闭标签页。")
" ⚠️ 当前查询范围内为空数据,终止并关闭标签页。"
)
page.locator(".tags-view-item", has_text="扫描记录查询").locator( page.locator(".tags-view-item", has_text="扫描记录查询").locator(
".el-icon-close" ".el-icon-close"
).click() ).click()
@@ -466,9 +457,7 @@ def yunda_actual_download(page):
try: try:
confirm_link.wait_for(state="visible", timeout=6000) confirm_link.wait_for(state="visible", timeout=6000)
if export_frame.get_by_text("导出任务建立成功").is_visible(): if export_frame.get_by_text("导出任务建立成功").is_visible():
print( print(" ✅ 导出任务已建立成功。")
" ✅ 导出任务已建立成功。"
)
confirm_link.click() confirm_link.click()
inner_success = True inner_success = True
break break
@@ -486,9 +475,7 @@ def yunda_actual_download(page):
export_frame.locator(".allRight").click() export_frame.locator(".allRight").click()
page.wait_for_timeout(500) page.wait_for_timeout(500)
else: else:
print( print(" ⚠️ 字段列表异常消失,重新打开导出面板...")
" ⚠️ 字段列表异常消失,重新打开导出面板..."
)
needs_reopen = True needs_reopen = True
break break
else: else:
@@ -531,7 +518,6 @@ def yunda_actual_download(page):
_yunda_poll_and_download_tasks( _yunda_poll_and_download_tasks(
page, page,
export_times, export_times,
target_task_title,
download_dir, download_dir,
"韵达-实到货物数据.xlsx", "韵达-实到货物数据.xlsx",
) )
@@ -541,9 +527,7 @@ def yunda_actual_download(page):
return False return False
def _yunda_poll_and_download_tasks( def _yunda_poll_and_download_tasks(page, export_times, download_dir, final_filename):
page, export_times, target_task_title, download_dir, final_filename
):
"""韵达离线任务的轮询与下载""" """韵达离线任务的轮询与下载"""
print("\n>> 正在前往【导出服务】界面...") print("\n>> 正在前往【导出服务】界面...")
yunda_smart_menu_click(page, ["基础数据", "导出服务"]) yunda_smart_menu_click(page, ["基础数据", "导出服务"])
@@ -567,20 +551,19 @@ def _yunda_poll_and_download_tasks(
ready_indices = [] ready_indices = []
processing_indices = [] processing_indices = []
# 单账号无并发:仅按创建时间容差(40s)认领本批任务,不再校验模块名称
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
for idx in range(row_count): for idx in range(row_count):
row = task_rows.nth(idx) row = task_rows.nth(idx)
module_name = row.locator("td[field='modueName']").inner_text().strip()
status_name = row.locator("td[field='fileStatus']").inner_text().strip() status_name = row.locator("td[field='fileStatus']").inner_text().strip()
create_time_str = ( create_time_str = (
row.locator("td[field='createdTime']").inner_text().strip() row.locator("td[field='createdTime']").inner_text().strip()
) )
if module_name == target_task_title:
try: try:
row_time = datetime.strptime(create_time_str, "%Y-%m-%d %H:%M:%S") row_time = datetime.strptime(create_time_str, "%Y-%m-%d %H:%M:%S")
matched = any( matched = any(
abs((row_time - et).total_seconds()) <= 60 abs((row_time - et).total_seconds()) <= 40 for et in export_times
for et in export_times
) )
if matched: if matched:

View File

@@ -49,7 +49,6 @@ def zto_smart_menu_click(page, menu_path):
def zto_expected_download(page): def zto_expected_download(page):
"""中通:应到货物数据下载""" """中通:应到货物数据下载"""
print("\n▶ 开始执行【中通 - 应到货物数据下载】任务...") print("\n▶ 开始执行【中通 - 应到货物数据下载】任务...")
target_task_title = "进站交接单查询-运单信息"
download_dir = DOWNLOAD_DIR download_dir = DOWNLOAD_DIR
if not os.path.exists(download_dir): if not os.path.exists(download_dir):
@@ -245,7 +244,6 @@ def zto_expected_download(page):
_zto_poll_and_download_tasks( _zto_poll_and_download_tasks(
page, page,
export_times, export_times,
target_task_title,
download_dir, download_dir,
"中通-应到货物数据.xlsx", "中通-应到货物数据.xlsx",
) )
@@ -258,7 +256,6 @@ def zto_expected_download(page):
def zto_actual_download(page): def zto_actual_download(page):
"""中通:实到货物数据下载""" """中通:实到货物数据下载"""
print("\n▶ 开始执行【中通 - 实到货物数据下载】任务...") print("\n▶ 开始执行【中通 - 实到货物数据下载】任务...")
target_task_title = "到件扫描管理"
download_dir = DOWNLOAD_DIR download_dir = DOWNLOAD_DIR
if not os.path.exists(download_dir): if not os.path.exists(download_dir):
@@ -340,9 +337,7 @@ def zto_actual_download(page):
"没有搜索到符合条件的数据记录" "没有搜索到符合条件的数据记录"
) )
if empty_flag.is_visible(): if empty_flag.is_visible():
print( print(" ⚠️ 当前查询范围内没有数据,终止并关闭标签页。")
" ⚠️ 当前查询范围内没有数据,终止并关闭标签页。"
)
try: try:
page.locator(".mini-tab", has_text="到件扫描监控").locator( page.locator(".mini-tab", has_text="到件扫描监控").locator(
".mini-tab-close" ".mini-tab-close"
@@ -404,7 +399,6 @@ def zto_actual_download(page):
_zto_poll_and_download_tasks( _zto_poll_and_download_tasks(
page, page,
export_times, export_times,
target_task_title,
download_dir, download_dir,
"中通-实到货物数据.xlsx", "中通-实到货物数据.xlsx",
) )
@@ -414,9 +408,7 @@ def zto_actual_download(page):
return False return False
def _zto_poll_and_download_tasks( def _zto_poll_and_download_tasks(page, export_times, download_dir, final_filename):
page, export_times, target_task_title, download_dir, final_filename
):
"""中通离线任务的轮询与下载""" """中通离线任务的轮询与下载"""
print("\n>> 正在前往【导出任务管理】界面...") print("\n>> 正在前往【导出任务管理】界面...")
zto_smart_menu_click(page, ["系统配置", "导出任务管理"]) zto_smart_menu_click(page, ["系统配置", "导出任务管理"])
@@ -439,21 +431,20 @@ def _zto_poll_and_download_tasks(
ready_timestamps = set() ready_timestamps = set()
processing_timestamps = set() processing_timestamps = set()
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
for i in range(row_count): for i in range(row_count):
tds = task_rows.nth(i).locator("td") tds = task_rows.nth(i).locator("td")
if tds.count() < 10: if tds.count() < 10:
continue continue
title_str = tds.nth(3).inner_text().strip()
submit_time_str = tds.nth(4).inner_text().strip() submit_time_str = tds.nth(4).inner_text().strip()
status_str = tds.nth(6).inner_text().strip() status_str = tds.nth(6).inner_text().strip()
if title_str == target_task_title:
try: try:
row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S") row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
matched = any( matched = any(
abs((row_time - et).total_seconds()) <= 20 abs((row_time - et).total_seconds()) <= 40 for et in export_times
for et in export_times
) )
if matched: if matched: