- Add paths.py: BASE_DIR/DOWNLOAD_DIR/CONFIG_PATH anchored on __file__ so paths resolve regardless of the launch cwd - Route main_router and all site modules through DOWNLOAD_DIR/CONFIG_PATH, replacing os.getcwd()-based download dirs and the "config.yaml" literal - main_router compare engine: read Excel as str with keep_default_na, strip/normalize 运单号, drop blanks, and isin against a set so int/float-vs-str mismatches no longer produce false "undelivered" - Shunxin: give downloaded files unique microsecond temp names and read Excel as str to preserve long-waybill precision - Normalize bare except: to except Exception: across affected files Co-Authored-By: Claude <noreply@anthropic.com>
104 lines
4.2 KiB
Python
104 lines
4.2 KiB
Python
# site_baishi.py
|
||
|
||
import os
|
||
import yaml
|
||
from playwright.sync_api import sync_playwright
|
||
|
||
from paths import DOWNLOAD_DIR, CONFIG_PATH
|
||
|
||
|
||
def baishi_download_undelivered_data(page):
|
||
"""百世:一键提取应到未到(当日未扫)数据"""
|
||
print("\n▶ 开始执行【百世 - 一键提取应到未到数据】任务...")
|
||
|
||
download_dir = DOWNLOAD_DIR
|
||
if not os.path.exists(download_dir):
|
||
os.makedirs(download_dir)
|
||
|
||
try:
|
||
# 1. 导航与页面加载
|
||
print(">> 正在进入【扫描综合查询】界面...")
|
||
# 打开基础服务菜单面板
|
||
page.locator("div.nav-level1", has_text="基础服务").click()
|
||
|
||
# 【重要修复】:通过限制在菜单面板(nav-level2-wrapper)内查找,
|
||
# 完美避开页面右侧同名 Tab 标签页的干扰
|
||
page.locator(".nav-level2-wrapper").locator(
|
||
"a", has_text="扫描综合查询"
|
||
).click()
|
||
|
||
# 验证表格主界面加载完毕
|
||
page.get_by_role("tab", name="实时扫描率").wait_for(state="visible")
|
||
print("✅ 扫描综合查询界面加载完毕")
|
||
|
||
# 2. 精准定位并点击【到/接件扫描 -> 当日 -> 未扫】的数字控件
|
||
print(">> 正在解析表格,提取当日到件未扫明细...")
|
||
# 表头结构复杂,精准定位第一行数据的第 13 列(索引 12)
|
||
target_cell = page.locator(".ant-table-tbody > tr").first.locator("td").nth(12)
|
||
|
||
# 特殊情况处理:检查未扫数量是否为0
|
||
cell_text = target_cell.inner_text().strip()
|
||
if cell_text == "0" or cell_text == "":
|
||
print(f" ℹ️ 注意:当日未扫数量为【{cell_text}】,无数据需要提取,任务结束。")
|
||
return
|
||
|
||
# 有未扫数据,继续点击操作
|
||
target_cell.locator("a").click()
|
||
|
||
# 等待下方弹出的明细表格区域加载完毕
|
||
detail_section = page.locator(".m-query-all-scanRateDetail")
|
||
detail_section.wait_for(state="visible")
|
||
page.wait_for_timeout(1000)
|
||
|
||
# 3. 触发导出设置模态框
|
||
print(">> 正在呼出导出配置面板...")
|
||
# 锁定在明细区域内的导出按钮,防止误点主表的导出
|
||
detail_section.locator(".export-wrap a[title='导出']").click()
|
||
|
||
# 验证模态框弹出
|
||
page.locator(".ant-modal-title", has_text="导出设置").wait_for(state="visible")
|
||
|
||
# 4. 加载 YAML 配置并输入密码
|
||
print(">> 正在读取本地配置文件并进行授权验证...")
|
||
password = ""
|
||
try:
|
||
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
||
config = yaml.safe_load(f)
|
||
password = config.get("baishi", {}).get("password", "")
|
||
|
||
if not password:
|
||
print(
|
||
" ⚠️ 警告:在 config.yaml 中未找到百世的登录密码,可能导致导出失败。"
|
||
)
|
||
except Exception as e:
|
||
print(f" ⚠️ 读取 config.yaml 失败,请确保文件存在且格式正确: {e}")
|
||
return
|
||
|
||
page.get_by_placeholder("请输入登录密码").fill(password)
|
||
|
||
# 5. 执行最终下载
|
||
print(">> 验证就绪,正在触发下载流...")
|
||
with page.expect_download() as download_info:
|
||
# 精准锁定模态框底部的“导 出”大按钮
|
||
page.locator(".ant-modal-footer").get_by_role(
|
||
"button", name="导 出"
|
||
).click()
|
||
|
||
download = download_info.value
|
||
save_path = os.path.join(download_dir, "百世-应到未到货物数据.xlsx")
|
||
|
||
# 如果之前已经有同名文件,覆盖保存
|
||
if os.path.exists(save_path):
|
||
os.remove(save_path)
|
||
|
||
download.save_as(save_path)
|
||
print(f"====================================================")
|
||
print(f" 🎉 恭喜!数据提取成功!")
|
||
print(f" ⬇️ 文件已落盘: {save_path}")
|
||
print(f"====================================================")
|
||
|
||
print("\n🎉 【百世 - 应到未到数据提取】全流程测试完毕!")
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ 任务执行过程中发生异常: {e}")
|