Files

105 lines
2.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""在已登录的探测浏览器(9333)上实测:交易流水页点「导出」→ 捕获下载文件。
只点导出按钮,不改筛选条件(沿用页面默认:今天 00:00~23:59)。
"""
import os
import sys
import time
try:
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
except Exception:
pass
from DrissionPage import ChromiumPage
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
DL_DIR = os.path.join(REPO_ROOT, 'logs', 'bankcomm_probe', 'downloads2')
os.makedirs(DL_DIR, exist_ok=True)
def log(m):
print(f'[{time.strftime("%H:%M:%S")}] {m}', flush=True)
page = ChromiumPage('127.0.0.1:9333')
log(f'当前 URL:{page.url}')
page.set.download_path(DL_DIR)
try:
page.set.when_download_file_exists('rename')
except Exception as e:
log(f'set.when_download_file_exists 失败:{e}')
btn = None
deadline = time.time() + 20
while time.time() < deadline and btn is None:
for b in page.eles('tag:button'):
try:
if (b.text or '').replace(' ', '') == '导出' and b.states.is_displayed:
btn = b
break
except Exception:
continue
if btn is None:
time.sleep(1)
if btn is None:
log('未找到「导出」按钮')
sys.exit(1)
log(f'找到导出按钮:{btn.text!r},点击')
try:
page.wait.download_begin(timeout=1) # 清掉可能的旧任务
except Exception:
pass
btn.click()
time.sleep(1)
# 可能弹确认框
for kw in ('确认', '确定', '下载'):
try:
m = page.ele(f'xpath://div[contains(@class,"ant-modal")]//button[contains(.,"{kw}")]', timeout=1)
if m:
log(f'弹出确认框,点击「{m.text}」')
m.click()
break
except Exception:
pass
log('等待浏览器下载…')
mission = None
for i in range(60): # 最多 120s
try:
mission = page.wait.download_begin(timeout=2)
if mission:
break
except Exception:
pass
try:
body = page.ele('css:.ant-message', timeout=0.3)
if body and body.text:
log(f'页面提示:{body.text}')
except Exception:
pass
time.sleep(0.5)
if not mission:
log('120s 内未捕获到下载;页面提示:')
try:
log(page.ele('css:.ant-message').text)
except Exception:
pass
sys.exit(1)
log(f'下载开始:{getattr(mission, "name", "?")}')
try:
mission.wait()
except Exception as e:
log(f'mission.wait 异常:{e}')
time.sleep(1)
log(f'下载结束,目录内容:{os.listdir(DL_DIR)}')
for n in os.listdir(DL_DIR):
p = os.path.join(DL_DIR, n)
log(f' {n} {os.path.getsize(p)} 字节')