import sys import time from pathlib import Path import datetime import calendar import json sys.path.insert(0, str(Path(__file__).parent)) from login import get_page from config import OUTPUT_DIR def navigate_to_report(page): print("正在打开主页...") page.get("https://yunmes.tftykj.cn/") page.wait.load_start() time.sleep(2) print("正在打开 生产工单发料异常检查报表...") try: m1 = page.ele('text=自定义报表管理') if m1: print("点击第一级...") m1.click() time.sleep(1) # 找到展开后的第二级 for m in page.eles('text:自定义报表管理'): try: m.click() except: pass time.sleep(1) for m in page.eles('text:自定义报表'): if m.text == '自定义报表': try: m.click() print("点击第三级...") except: pass time.sleep(2) ele = page.ele('text:生产工单发料异常检查报表', timeout=5) if ele: print("找到报表行,选中...") ele.parent('tag:tr').click() time.sleep(0.5) btn = page.ele('text=进入自定义报表') if btn: print("点击进入自定义报表...") btn.click() time.sleep(3) print("成功进入报表!") return True else: print("未找到进入按钮。") return False else: print("未能找到 '生产工单发料异常检查报表'") return False except Exception as e: print(f"执行导航过程中发生异常: {e}") return False def fetch_report_data(page): # Wait for the new tab to be ready time.sleep(3) target_tab = page.get_tab(page.latest_tab) # Wait for the label to appear target_tab.ele('text:下单日期(开始)', timeout=10) now = datetime.datetime.now() first_day = datetime.date(now.year, now.month, 1).strftime('%Y-%m-%d') last_day = datetime.date(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime('%Y-%m-%d') print(f"设置下单日期为当月: {first_day} 至 {last_day},并清理发料情况过滤条件...") # 使用注入到全部 iframe 的 JS 强制执行 EasyUI 方法 target_tab.run_js(f""" var iframes = document.querySelectorAll('iframe'); for(var j=0; j 0) {{ win.$(startInputs[0]).datebox('setValue', '{first_day}'); }} // 2. 设置结束日期 var endInputs = doc.querySelectorAll('.input_EndValue.datebox-f'); if (endInputs.length > 0) {{ win.$(endInputs[0]).datebox('setValue', '{last_day}'); }} // 3. 清理所有下拉框(包括发料情况) var combos = doc.querySelectorAll('.combobox-f, .textbox-f'); for(var i=0; i 0 else 1 # Import and save to database try: import import_to_sqlite if items: inserted = import_to_sqlite.import_abnormal_report_data(items) total_inserted += inserted print(f"✅ 成功将本页 {inserted} 条异常报表数据存入数据库") except Exception as db_err: print(f"❌ 保存异常报表数据到数据库失败: {db_err}") found_data = True else: print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] 数据结构不匹配。") except Exception as e: print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] 解析数据包出错: {e}") pass if not found_data: print(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] 第 {current_page} 页等待了3秒,没有拦截到匹配的报表数据...") # 再给一次机会等3秒 print("再等待3秒重试...") time.sleep(3) retry_packets = target_tab.listen.steps() print(f"重试收集到 {len(retry_packets)} 个数据包。") if not retry_packets: print(f"彻底没有数据,停止抓取。") break else: packets.extend(retry_packets) # 重新让上面解析 continue if current_page >= total_pages: print(f"已到达最后一页 (共 {total_pages} 页),抓取完成!") break print(f"准备抓取下一页 (第 {current_page + 1} 页)...") time.sleep(1) # 尝试点击下一页 (同样需要穿透 iframe) target_tab.run_js(""" var iframes = document.querySelectorAll('iframe'); for(var j=0; j