Unify export-task matching to time tolerance only (≤40s)
去掉顺心/中通/韵达/安能(应到) 导出任务认领时的标题/模块名校验, 统一改为仅按提交时间容差 ≤40 秒匹配本批任务。 理由:单账号无并发,时间窗内的任务必为本批所建;而站点可能调整 任务标题名,写死标题会导致匹配失败、任务已创建却一直查不到。 同步清理因此变成死代码的局部变量与函数形参。 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -52,8 +52,6 @@ def set_cdp_port(port):
|
||||
CDP_PORT = int(port)
|
||||
|
||||
|
||||
# 导出下载页里,本批任务的“导出功能名称”固定为“交接单明细”。
|
||||
TARGET_EXPORT_FUNC = "交接单明细"
|
||||
FINAL_FILENAME = "安能-应到货物数据.xlsx"
|
||||
|
||||
# 进站交接单查询页 / 导出下载页的 URL 特征(用于在 /json 里定位/复用 tab 目标)。
|
||||
@@ -651,18 +649,20 @@ def _click_export_query(export_cdp):
|
||||
|
||||
|
||||
def _match_our_tasks(rows, export_times):
|
||||
"""从行里挑出本批任务(导出功能名称=交接单明细 且 导出时间接近某次导出时刻)。"""
|
||||
"""从行里挑出本批任务:仅按导出时间容差(40s)匹配,不校验导出功能名称。
|
||||
|
||||
单账号无并发,本批提交的任务必为我们创建;而站点可能调整导出功能名称,
|
||||
写死名称反而会导致匹配失败、任务一直查不到。
|
||||
"""
|
||||
matched = []
|
||||
for row in rows:
|
||||
cell = row.get("cellMap", {})
|
||||
if cell.get("导出功能名称") != TARGET_EXPORT_FUNC:
|
||||
continue
|
||||
time_str = cell.get("导出时间", "")
|
||||
try:
|
||||
row_time = datetime.strptime(time_str.strip(), "%Y-%m-%d %H:%M:%S")
|
||||
except ValueError:
|
||||
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
|
||||
matched.append(row)
|
||||
return matched
|
||||
@@ -672,7 +672,7 @@ def poll_and_download_tasks(export_cdp, export_times, download_dir):
|
||||
"""轮询导出下载页直到本批任务齐全且全部“导出完成”,再逐个免对话框下载。"""
|
||||
total_expected = len(export_times)
|
||||
|
||||
print(f">> 轮询导出下载任务(期望 {total_expected} 个“{TARGET_EXPORT_FUNC}”)...")
|
||||
print(f">> 轮询导出下载任务(期望 {total_expected} 个)...")
|
||||
while True:
|
||||
wait_until(
|
||||
export_cdp,
|
||||
|
||||
@@ -206,37 +206,35 @@ def shunxin_expected_download(page):
|
||||
pending_tasks = 0
|
||||
current_ready_timestamps = []
|
||||
|
||||
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
|
||||
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
|
||||
for i in range(row_count):
|
||||
tds = rows.nth(i).locator("td")
|
||||
if tds.count() < 9:
|
||||
continue
|
||||
|
||||
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()
|
||||
|
||||
if title_str == "发车管理运单列表":
|
||||
try:
|
||||
row_time = datetime.strptime(
|
||||
submit_time_str, "%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 60
|
||||
for et in export_times
|
||||
)
|
||||
try:
|
||||
row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 40
|
||||
for et in export_times
|
||||
)
|
||||
|
||||
if matched:
|
||||
if status_str != "执行完成":
|
||||
pending_tasks += 1
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
print(
|
||||
f" ⏳ 任务 [{submit_time_str}] 状态为【{status_str}】,数据生成中..."
|
||||
)
|
||||
else:
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
current_ready_timestamps.append(submit_time_str)
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 解析时间时出错: {e}")
|
||||
if matched:
|
||||
if status_str != "执行完成":
|
||||
pending_tasks += 1
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
print(
|
||||
f" ⏳ 任务 [{submit_time_str}] 状态为【{status_str}】,数据生成中..."
|
||||
)
|
||||
else:
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
current_ready_timestamps.append(submit_time_str)
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 解析时间时出错: {e}")
|
||||
|
||||
if pending_tasks > 0:
|
||||
print(
|
||||
@@ -454,37 +452,35 @@ def shunxin_actual_download(page):
|
||||
pending_tasks = 0
|
||||
current_ready_timestamps = []
|
||||
|
||||
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
|
||||
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
|
||||
for i in range(row_count):
|
||||
tds = rows.nth(i).locator("td")
|
||||
if tds.count() < 9:
|
||||
continue
|
||||
|
||||
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()
|
||||
|
||||
if title_str.startswith("卸车扫描记录"):
|
||||
try:
|
||||
row_time = datetime.strptime(
|
||||
submit_time_str, "%Y-%m-%d %H:%M:%S"
|
||||
)
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 60
|
||||
for et in export_times
|
||||
)
|
||||
try:
|
||||
row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 40
|
||||
for et in export_times
|
||||
)
|
||||
|
||||
if matched:
|
||||
if status_str != "执行完成":
|
||||
pending_tasks += 1
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
print(
|
||||
f" ⏳ 任务 [{submit_time_str}] 状态为【{status_str}】,数据生成中..."
|
||||
)
|
||||
else:
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
current_ready_timestamps.append(submit_time_str)
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 解析时间时出错: {e}")
|
||||
if matched:
|
||||
if status_str != "执行完成":
|
||||
pending_tasks += 1
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
print(
|
||||
f" ⏳ 任务 [{submit_time_str}] 状态为【{status_str}】,数据生成中..."
|
||||
)
|
||||
else:
|
||||
if submit_time_str not in current_ready_timestamps:
|
||||
current_ready_timestamps.append(submit_time_str)
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 解析时间时出错: {e}")
|
||||
|
||||
if pending_tasks > 0:
|
||||
print(
|
||||
|
||||
@@ -81,7 +81,6 @@ def yunda_smart_menu_click(page, menu_path):
|
||||
def yunda_expected_download(page):
|
||||
"""韵达:应到货物数据下载"""
|
||||
print("\n▶ 开始执行【韵达 - 应到货物数据下载】任务...")
|
||||
target_task_title = "进站主单表"
|
||||
|
||||
download_dir = DOWNLOAD_DIR
|
||||
if not os.path.exists(download_dir):
|
||||
@@ -245,9 +244,7 @@ def yunda_expected_download(page):
|
||||
try:
|
||||
confirm_link.wait_for(state="visible", timeout=6000)
|
||||
if export_frame.get_by_text("导出任务建立成功").is_visible():
|
||||
print(
|
||||
" ✅ 导出任务已建立成功。"
|
||||
)
|
||||
print(" ✅ 导出任务已建立成功。")
|
||||
confirm_link.click()
|
||||
inner_success = True
|
||||
break
|
||||
@@ -311,7 +308,6 @@ def yunda_expected_download(page):
|
||||
_yunda_poll_and_download_tasks(
|
||||
page,
|
||||
export_times,
|
||||
target_task_title,
|
||||
download_dir,
|
||||
"韵达-应到货物数据.xlsx",
|
||||
)
|
||||
@@ -324,7 +320,6 @@ def yunda_expected_download(page):
|
||||
def yunda_actual_download(page):
|
||||
"""韵达:实到货物数据下载"""
|
||||
print("\n▶ 开始执行【韵达 - 实到货物数据下载】任务...")
|
||||
target_task_title = "扫描记录数据"
|
||||
|
||||
download_dir = 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)
|
||||
if match_total and int(match_total.group(1)) > 0:
|
||||
has_records = True
|
||||
print(
|
||||
f" ✅ 实到数据已加载,总记录数: [{match_total.group(1)}] 条。"
|
||||
)
|
||||
print(f" ✅ 实到数据已加载,总记录数: [{match_total.group(1)}] 条。")
|
||||
|
||||
if not has_records:
|
||||
if ws_frame.locator(
|
||||
".no-records-found", has_text="没有找到匹配的记录"
|
||||
).first.is_visible():
|
||||
print(
|
||||
" ⚠️ 当前查询范围内为空数据,终止并关闭标签页。"
|
||||
)
|
||||
print(" ⚠️ 当前查询范围内为空数据,终止并关闭标签页。")
|
||||
page.locator(".tags-view-item", has_text="扫描记录查询").locator(
|
||||
".el-icon-close"
|
||||
).click()
|
||||
@@ -466,9 +457,7 @@ def yunda_actual_download(page):
|
||||
try:
|
||||
confirm_link.wait_for(state="visible", timeout=6000)
|
||||
if export_frame.get_by_text("导出任务建立成功").is_visible():
|
||||
print(
|
||||
" ✅ 导出任务已建立成功。"
|
||||
)
|
||||
print(" ✅ 导出任务已建立成功。")
|
||||
confirm_link.click()
|
||||
inner_success = True
|
||||
break
|
||||
@@ -486,9 +475,7 @@ def yunda_actual_download(page):
|
||||
export_frame.locator(".allRight").click()
|
||||
page.wait_for_timeout(500)
|
||||
else:
|
||||
print(
|
||||
" ⚠️ 字段列表异常消失,重新打开导出面板..."
|
||||
)
|
||||
print(" ⚠️ 字段列表异常消失,重新打开导出面板...")
|
||||
needs_reopen = True
|
||||
break
|
||||
else:
|
||||
@@ -531,7 +518,6 @@ def yunda_actual_download(page):
|
||||
_yunda_poll_and_download_tasks(
|
||||
page,
|
||||
export_times,
|
||||
target_task_title,
|
||||
download_dir,
|
||||
"韵达-实到货物数据.xlsx",
|
||||
)
|
||||
@@ -541,9 +527,7 @@ def yunda_actual_download(page):
|
||||
return False
|
||||
|
||||
|
||||
def _yunda_poll_and_download_tasks(
|
||||
page, export_times, target_task_title, download_dir, final_filename
|
||||
):
|
||||
def _yunda_poll_and_download_tasks(page, export_times, download_dir, final_filename):
|
||||
"""韵达离线任务的轮询与下载"""
|
||||
print("\n>> 正在前往【导出服务】界面...")
|
||||
yunda_smart_menu_click(page, ["基础数据", "导出服务"])
|
||||
@@ -567,29 +551,28 @@ def _yunda_poll_and_download_tasks(
|
||||
ready_indices = []
|
||||
processing_indices = []
|
||||
|
||||
# 单账号无并发:仅按创建时间容差(40s)认领本批任务,不再校验模块名称
|
||||
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
|
||||
for idx in range(row_count):
|
||||
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()
|
||||
create_time_str = (
|
||||
row.locator("td[field='createdTime']").inner_text().strip()
|
||||
)
|
||||
|
||||
if module_name == target_task_title:
|
||||
try:
|
||||
row_time = datetime.strptime(create_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 60
|
||||
for et in export_times
|
||||
)
|
||||
try:
|
||||
row_time = datetime.strptime(create_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 40 for et in export_times
|
||||
)
|
||||
|
||||
if matched:
|
||||
if status_name == "导出完成":
|
||||
ready_indices.append(idx)
|
||||
else:
|
||||
processing_indices.append(idx)
|
||||
except Exception:
|
||||
pass
|
||||
if matched:
|
||||
if status_name == "导出完成":
|
||||
ready_indices.append(idx)
|
||||
else:
|
||||
processing_indices.append(idx)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
total_found = len(ready_indices) + len(processing_indices)
|
||||
print(
|
||||
|
||||
41
site_zto.py
41
site_zto.py
@@ -49,7 +49,6 @@ def zto_smart_menu_click(page, menu_path):
|
||||
def zto_expected_download(page):
|
||||
"""中通:应到货物数据下载"""
|
||||
print("\n▶ 开始执行【中通 - 应到货物数据下载】任务...")
|
||||
target_task_title = "进站交接单查询-运单信息"
|
||||
|
||||
download_dir = DOWNLOAD_DIR
|
||||
if not os.path.exists(download_dir):
|
||||
@@ -245,7 +244,6 @@ def zto_expected_download(page):
|
||||
_zto_poll_and_download_tasks(
|
||||
page,
|
||||
export_times,
|
||||
target_task_title,
|
||||
download_dir,
|
||||
"中通-应到货物数据.xlsx",
|
||||
)
|
||||
@@ -258,7 +256,6 @@ def zto_expected_download(page):
|
||||
def zto_actual_download(page):
|
||||
"""中通:实到货物数据下载"""
|
||||
print("\n▶ 开始执行【中通 - 实到货物数据下载】任务...")
|
||||
target_task_title = "到件扫描管理"
|
||||
|
||||
download_dir = DOWNLOAD_DIR
|
||||
if not os.path.exists(download_dir):
|
||||
@@ -340,9 +337,7 @@ def zto_actual_download(page):
|
||||
"没有搜索到符合条件的数据记录"
|
||||
)
|
||||
if empty_flag.is_visible():
|
||||
print(
|
||||
" ⚠️ 当前查询范围内没有数据,终止并关闭标签页。"
|
||||
)
|
||||
print(" ⚠️ 当前查询范围内没有数据,终止并关闭标签页。")
|
||||
try:
|
||||
page.locator(".mini-tab", has_text="到件扫描监控").locator(
|
||||
".mini-tab-close"
|
||||
@@ -404,7 +399,6 @@ def zto_actual_download(page):
|
||||
_zto_poll_and_download_tasks(
|
||||
page,
|
||||
export_times,
|
||||
target_task_title,
|
||||
download_dir,
|
||||
"中通-实到货物数据.xlsx",
|
||||
)
|
||||
@@ -414,9 +408,7 @@ def zto_actual_download(page):
|
||||
return False
|
||||
|
||||
|
||||
def _zto_poll_and_download_tasks(
|
||||
page, export_times, target_task_title, download_dir, final_filename
|
||||
):
|
||||
def _zto_poll_and_download_tasks(page, export_times, download_dir, final_filename):
|
||||
"""中通离线任务的轮询与下载"""
|
||||
print("\n>> 正在前往【导出任务管理】界面...")
|
||||
zto_smart_menu_click(page, ["系统配置", "导出任务管理"])
|
||||
@@ -439,30 +431,29 @@ def _zto_poll_and_download_tasks(
|
||||
ready_timestamps = set()
|
||||
processing_timestamps = set()
|
||||
|
||||
# 单账号无并发:仅按提交时间容差(40s)认领本批任务,不再校验任务标题
|
||||
# (站点可能调整标题名,写死标题会导致匹配失败、任务一直查不到)。
|
||||
for i in range(row_count):
|
||||
tds = task_rows.nth(i).locator("td")
|
||||
if tds.count() < 10:
|
||||
continue
|
||||
|
||||
title_str = tds.nth(3).inner_text().strip()
|
||||
submit_time_str = tds.nth(4).inner_text().strip()
|
||||
status_str = tds.nth(6).inner_text().strip()
|
||||
|
||||
if title_str == target_task_title:
|
||||
try:
|
||||
row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 20
|
||||
for et in export_times
|
||||
)
|
||||
try:
|
||||
row_time = datetime.strptime(submit_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
matched = any(
|
||||
abs((row_time - et).total_seconds()) <= 40 for et in export_times
|
||||
)
|
||||
|
||||
if matched:
|
||||
if status_str == "成功执行":
|
||||
ready_timestamps.add(submit_time_str)
|
||||
else:
|
||||
processing_timestamps.add(submit_time_str)
|
||||
except Exception:
|
||||
pass
|
||||
if matched:
|
||||
if status_str == "成功执行":
|
||||
ready_timestamps.add(submit_time_str)
|
||||
else:
|
||||
processing_timestamps.add(submit_time_str)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
total_found = len(ready_timestamps) + len(processing_timestamps)
|
||||
print(
|
||||
|
||||
Reference in New Issue
Block a user