很精准
import cv2
import numpy as np
import pyautogui
import time
# 加载“关注”按钮的模板图像
follow_button_template = cv2.imread('dy3.png', 0) # 替换为你的“关注”按钮截图
# 设置匹配阈值(根据实际情况调整)
threshold = 0.8
def scroll_down():
pyautogui.scroll(-100) # 向下滚动页面
time.sleep(1) # 等待页面加载
def find_and_click_button(template):
# 截取当前屏幕
screenshot = pyautogui.screenshot()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
# 使用模板匹配查找“关注”按钮
result = cv2.matchTemplate(screenshot, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 如果匹配度超过阈值,则认为找到了按钮
if max_val >= threshold:
# 获取按钮的中心位置
h, w = template.shape
button_center = (max_loc[0] + w // 2, max_loc[1] + h // 2)
# 移动鼠标到按钮位置并点击
pyautogui.moveTo(button_center[0], button_center[1], duration=0.5)
pyautogui.click()
print(f"找到并点击了按钮,位置: {button_center}")
return True
else:
print("未找到按钮")
return False
# 在主循环中加入滚动逻辑
def main():
print("开始批量取消关注...")
while True:
if not find_and_click_button(follow_button_template):
print("没有更多按钮可点击,尝试滚动页面...")
scroll_down()
else:
time.sleep(1) # 避免过快点击导致误操作
if __name__ == "__main__":
main()