'commit'
This commit is contained in:
@@ -55,38 +55,50 @@ class TeLaiDianCrawler(BaseCrawler):
|
||||
|
||||
async def clear_ads(self, d, max_rounds=3):
|
||||
"""
|
||||
清理页面上的广告弹窗
|
||||
清理页面上的广告弹窗,支持多轮检测
|
||||
"""
|
||||
logger.info(f"开始清理广告弹窗,最多尝试 {max_rounds} 轮...")
|
||||
logger.info(f"--- [广告清理] 开始检测,最多尝试 {max_rounds} 轮 ---")
|
||||
for i in range(max_rounds):
|
||||
ad_screen = take_screenshot(d, f"tld_ad_check_{int(time.time())}.jpg")
|
||||
res = await self.read_image_kit.find_close_button_vlm(ad_screen)
|
||||
round_idx = i + 1
|
||||
logger.info(f"[广告清理] 第 {round_idx} 轮:正在截屏分析...")
|
||||
ad_screen = take_screenshot(d, f"tld_ad_check_r{round_idx}_{int(time.time())}.jpg")
|
||||
|
||||
if res.get("has_ad") and res.get("close_point"):
|
||||
close_point = res.get("close_point")
|
||||
w, h = d.window_size()
|
||||
target_x = int(close_point[0] * w / 1000)
|
||||
target_y = int(close_point[1] * h / 1000)
|
||||
try:
|
||||
res = await self.read_image_kit.find_close_button_vlm(ad_screen)
|
||||
|
||||
# 安全校验:绝对不能点击微信小程序的胶囊按钮区 (右上角)
|
||||
# 胶囊按钮通常在 x > 80% 宽度 且 y < 100 像素(或 8% 高度) 的区域
|
||||
if target_x > w * 0.75 and target_y < 150:
|
||||
logger.warning(f"⚠️ 拒绝点击疑似微信胶囊按钮的区域: ({target_x}, {target_y})")
|
||||
continue
|
||||
if res.get("has_ad") and res.get("close_point"):
|
||||
close_point = res.get("close_point")
|
||||
reason = res.get("reason", "未提供原因")
|
||||
w, h = d.window_size()
|
||||
target_x = int(close_point[0] * w / 1000)
|
||||
target_y = int(close_point[1] * h / 1000)
|
||||
|
||||
logger.info(f"[广告清理] 第 {round_idx} 轮:VLM 发现广告!原因: {reason}")
|
||||
logger.info(f"[广告清理] 计划点击坐标: ({target_x}, {target_y}),归一化坐标: {close_point}")
|
||||
|
||||
# 安全校验:绝对不能点击微信小程序的胶囊按钮区 (右上角)
|
||||
if target_x > w * 0.75 and target_y < 150:
|
||||
logger.warning(f"[广告清理] ⚠️ 拒绝点击疑似微信胶囊按钮的区域: ({target_x}, {target_y}),跳过本轮。")
|
||||
continue
|
||||
|
||||
logger.info(f"第 {i+1} 轮发现广告: {res.get('reason')},点击关闭: ({target_x}, {target_y})")
|
||||
d.click(target_x, target_y)
|
||||
await asyncio.sleep(1.5)
|
||||
else:
|
||||
logger.info(f"第 {i+1} 轮未发现明显广告,清理结束。")
|
||||
logger.info(f"[广告清理] 正在执行点击关闭操作...")
|
||||
d.click(target_x, target_y)
|
||||
# 点击后等待一下,让弹窗消失或下一轮广告弹出
|
||||
await asyncio.sleep(2.0)
|
||||
else:
|
||||
logger.info(f"[广告清理] 第 {round_idx} 轮:未发现广告弹窗。VLM 理由: {res.get('reason', '无')}")
|
||||
if os.path.exists(ad_screen): os.remove(ad_screen)
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"[广告清理] 第 {round_idx} 轮检测发生异常: {e}")
|
||||
finally:
|
||||
if os.path.exists(ad_screen): os.remove(ad_screen)
|
||||
break
|
||||
|
||||
if os.path.exists(ad_screen): os.remove(ad_screen)
|
||||
|
||||
logger.info("--- [广告清理] 任务结束 ---")
|
||||
|
||||
async def crawl_list_logic(self, d):
|
||||
# [临时禁用] 广告清理逻辑误触严重,先解决主流程问题
|
||||
# await self.clear_ads(d)
|
||||
# 1. 启动即清理广告 (已根据要求关闭)
|
||||
# await self.clear_ads(d, max_rounds=3)
|
||||
|
||||
# [优化] 向下滚动以刷新/校准地理位置
|
||||
# 使用更加显式的 swipe 方式:从屏幕 30% 划到 80%
|
||||
@@ -282,68 +294,75 @@ class TeLaiDianCrawler(BaseCrawler):
|
||||
|
||||
w, h = d.window_size()
|
||||
|
||||
logger.info("[详情页] 根据用户策略: 多次大幅向上滑动,直到页面基本不再变化")
|
||||
last_md5 = None
|
||||
stable_count = 0
|
||||
max_round = 30
|
||||
final_screen_path = None
|
||||
logger.info("[详情页] 根据用户要求:进入页面后多等会,然后向上滑动一整屏,通过 VLM 查找价格入口")
|
||||
w, h = d.window_size()
|
||||
|
||||
# 1. 增加等待时间,确保页面加载完成
|
||||
logger.info(f"[详情页] 等待 {WAIT_DETAIL_PAGE_LOAD + 2}s 确保页面稳定 (根据要求多等会)...")
|
||||
await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD + 2)
|
||||
|
||||
# 2. 向上滑动一部分屏 (从屏幕底部的 85% 划到顶部的 15%)
|
||||
# [优化] 避免滑过头,从全屏改为 0.7 屏幕左右
|
||||
logger.info("[详情页] 执行向上滑动操作 (y: 85% -> 15%)")
|
||||
d.swipe(w // 2, int(h * 0.85), w // 2, int(h * 0.15), duration=0.8)
|
||||
await asyncio.sleep(2.0) # 滑动后再次等待稳定
|
||||
|
||||
from Apps.TeLaiDian.Kit import get_image_content_md5
|
||||
# 3. 截图并识别
|
||||
final_screen_path = take_screenshot(d, f"tld_detail_vlm_input_{int(time.time())}.jpg")
|
||||
logger.info(f"[详情页] 已截取 VLM 识别用图: {final_screen_path}")
|
||||
|
||||
for idx in range(max_round):
|
||||
start_x = int(w * 0.9)
|
||||
start_y = int(h * 0.85)
|
||||
end_y = int(h * 0.25)
|
||||
logger.info(f"[详情页] 第 {idx + 1} 轮大幅向上滑动: ({start_x}, {start_y}) -> ({start_x}, {end_y})")
|
||||
d.swipe(start_x, start_y, start_x, end_y, 0.25)
|
||||
await asyncio.sleep(1.0)
|
||||
|
||||
screen_path = take_screenshot(d, f"tld_detail_scan_{int(time.time())}_{idx}.jpg")
|
||||
logger.info(f"[详情页] 第 {idx + 1} 轮滑动后的截图: {screen_path}")
|
||||
curr_md5 = get_image_content_md5(
|
||||
screen_path,
|
||||
top_ratio=SAFE_EXCLUDE_RATIO,
|
||||
bottom_ratio=BOTTOM_SAFE_EXCLUDE_RATIO,
|
||||
)
|
||||
if last_md5 is not None and curr_md5 == last_md5:
|
||||
stable_count += 1
|
||||
logger.info(f"[详情页] 页面内容连续第 {stable_count} 次无变化,可能已到稳定区域")
|
||||
else:
|
||||
stable_count = 0
|
||||
last_md5 = curr_md5
|
||||
final_screen_path = screen_path
|
||||
|
||||
if stable_count >= 2:
|
||||
logger.info("[详情页] 检测到页面多次无变化,认为已到达顶部固定区域,提前结束扫描。")
|
||||
break
|
||||
|
||||
if not final_screen_path:
|
||||
final_screen_path = take_screenshot(d, f"tld_detail_scan_final_{int(time.time())}.jpg")
|
||||
logger.info(f"[详情页] 扫描流程未生成截图,使用兜底截图: {final_screen_path}")
|
||||
else:
|
||||
logger.info(f"[详情页] 使用最终稳定区域截图作为价格页识别输入: {final_screen_path}")
|
||||
|
||||
logger.info("[详情页] 使用固定归一化坐标点击顶部“价格”标签,并点击左侧当前价数字进入全时段电价")
|
||||
logger.info("[详情页] 优先使用 VLM 识别顶部“价格”标签和价格入口")
|
||||
price_tab_screen = final_screen_path
|
||||
entrance_clicked = False
|
||||
|
||||
try:
|
||||
tab_x = int(PRICE_TAB_X_NORM * w / 1000)
|
||||
tab_y = int(PRICE_TAB_Y_NORM * h / 1000)
|
||||
logger.info(f"[详情页] 固定坐标点击价格标签: 归一化({PRICE_TAB_X_NORM}, {PRICE_TAB_Y_NORM}) -> 像素({tab_x}, {tab_y})")
|
||||
# 1. 尝试使用 VLM 寻找顶部“价格”标签
|
||||
tab_data = await self.read_image_kit.find_price_tab_vlm(final_screen_path)
|
||||
tab_x, tab_y = None, None
|
||||
|
||||
if tab_data.get("found") and tab_data.get("point"):
|
||||
p = tab_data["point"]
|
||||
tab_x = int(p[0] * w / 1000)
|
||||
tab_y = int(p[1] * h / 1000)
|
||||
logger.info(f"[详情页] VLM 成功找到价格标签: 归一化{p} -> 像素({tab_x}, {tab_y})")
|
||||
else:
|
||||
tab_x = int(PRICE_TAB_X_NORM * w / 1000)
|
||||
tab_y = int(PRICE_TAB_Y_NORM * h / 1000)
|
||||
logger.warning(f"[详情页] VLM 未找到价格标签,使用固定坐标兜底: 像素({tab_x}, {tab_y})")
|
||||
|
||||
logger.info(f"[详情页] 正在点击价格标签...")
|
||||
d.click(tab_x, tab_y)
|
||||
await asyncio.sleep(1.0)
|
||||
await asyncio.sleep(2.0) # 点击标签后多等会
|
||||
|
||||
price_tab_screen = take_screenshot(d, f"tld_detail_after_price_tab_{int(time.time())}.jpg")
|
||||
logger.info(f"[详情页] 点击价格标签后的界面截图已保存: {price_tab_screen}")
|
||||
|
||||
entry_x = int(PRICE_ENTRY_X_NORM * w / 1000)
|
||||
entry_y = int(PRICE_ENTRY_Y_NORM * h / 1000)
|
||||
# 2. 尝试使用 VLM 寻找价格入口 (重点识别“全部时段 >”)
|
||||
entry_data = await self.read_image_kit.find_price_entrance_vlm(price_tab_screen)
|
||||
entry_x, entry_y = None, None
|
||||
|
||||
if entry_data.get("found") and entry_data.get("point"):
|
||||
p = entry_data["point"]
|
||||
# 安全校验:严禁点击 y > 850 (85%) 的区域,那是底部浮动条
|
||||
if p[1] > 850:
|
||||
logger.warning(f"[详情页] VLM 虽返回坐标 {p},但 Y 轴过大 (疑似底部浮动条),强制判定为未找到。")
|
||||
entry_x = int(PRICE_ENTRY_X_NORM * w / 1000)
|
||||
entry_y = int(PRICE_ENTRY_Y_NORM * h / 1000)
|
||||
else:
|
||||
entry_x = int(p[0] * w / 1000)
|
||||
entry_y = int(p[1] * h / 1000)
|
||||
logger.info(f"[详情页] VLM 成功找到价格入口 (匹配全部时段标识): 归一化{p} -> 像素({entry_x}, {entry_y}),原因: {entry_data.get('reason')}")
|
||||
else:
|
||||
entry_x = int(PRICE_ENTRY_X_NORM * w / 1000)
|
||||
entry_y = int(PRICE_ENTRY_Y_NORM * h / 1000)
|
||||
logger.warning(f"[详情页] VLM 未找到价格入口,使用固定坐标兜底: 像素({entry_x}, {entry_y})")
|
||||
|
||||
click_x = max(5, min(w - 5, entry_x))
|
||||
click_y = max(5, min(h - 5, entry_y))
|
||||
|
||||
debug_click_path = price_tab_screen.replace(
|
||||
".jpg",
|
||||
f"_click_{tab_x}_{tab_y}_price_{click_x}_{click_y}.jpg"
|
||||
f"_click_tab_{tab_x}_{tab_y}_entry_{click_x}_{click_y}.jpg"
|
||||
)
|
||||
try:
|
||||
img = read_image(price_tab_screen)
|
||||
@@ -351,18 +370,16 @@ class TeLaiDianCrawler(BaseCrawler):
|
||||
cv2.circle(img, (tab_x, tab_y), 20, (0, 0, 255), -1)
|
||||
cv2.circle(img, (click_x, click_y), 20, (0, 0, 255), -1)
|
||||
save_image(debug_click_path, img)
|
||||
logger.info(f"[详情页] 已生成价格标签与当前价入口红点标记图: {debug_click_path}")
|
||||
else:
|
||||
logger.warning(f"[详情页] 加载价格页截图失败,无法绘制诊断红点: {price_tab_screen}")
|
||||
logger.info(f"[详情页] 已生成价格点击诊断红点图: {debug_click_path}")
|
||||
except Exception as e:
|
||||
logger.error(f"[详情页] 生成价格入口点击诊断图片失败: {e}")
|
||||
logger.error(f"[详情页] 生成诊断图片失败: {e}")
|
||||
|
||||
logger.info(f"[详情页] 点击当前价入口: 像素({click_x}, {click_y}),屏幕大小: ({w}, {h})")
|
||||
logger.info(f"[详情页] 正在点击电价入口...")
|
||||
d.click(click_x, click_y)
|
||||
entrance_clicked = True
|
||||
await asyncio.sleep(WAIT_DETAIL_PAGE_LOAD)
|
||||
except Exception as e:
|
||||
logger.error(f"[详情页] 固定坐标点击价格标签或入口失败: {e}")
|
||||
logger.error(f"[详情页] 识别或点击价格入口失败: {e}")
|
||||
|
||||
if entrance_clicked:
|
||||
entered_price_path = take_screenshot(d, f"tld_detail_price_after_enter_{int(time.time())}.jpg")
|
||||
|
||||
Reference in New Issue
Block a user