import pyautogui
import time
import random
import inspect
import os
platform = "toutiao" # 可以设置为 "twitter", 或 "toutiao"
prefix = 'pic/toutiao/'
image_paths = {
"toutiao": [prefix + "favorite.png", prefix + "headfavar.png"] # 头条点赞
}
image_path, next_pic = image_paths.get(platform, [None, None])
failedImage ='failedImage.png'
x, y = None, None
counter = 1008000
clickcounter =0;
delay = 2
scolltime = 30 # 滚动次数
refresh_count = 0
def debug_print(message):
frame = inspect.currentframe().f_back
line_number = frame.f_lineno
file_name = inspect.getframeinfo(frame).filename
print(f"[DEBUG] {file_name}, Line {line_number}: {message}")
if __name__ == "__main__":
while counter > 0:
try:
time.sleep(delay) # 确保点击的按钮是正确的坐标
x, y = pyautogui.locateCenterOnScreen(image_path)
# 找到图片后点击,并将计数器减1
pyautogui.click(x, y)
except:
my_array = [random.randint(-350, -100) for i in range(scolltime)]
count = 0
for i in range(len(my_array)):
random_index = random.randint(0, len(my_array) - 1)
print(my_array[random_index])
pyautogui.scroll( my_array[random_index]) # 向下滚动50个像素
try:
time.sleep(delay)
x, y = pyautogui.locateCenterOnScreen(image_path)
pyautogui.click(x, y)
counter -= 1
clickcounter += 1
print(f"找到了图片,还剩{counter}次机会 (Line {line_number})") # 如果未找到图片,则进行刷新操作
print(f"找到了图片,已经点击了{clickcounter}次 (Line {line_number})")
break
except Exception as e:
print(f"未执行滚动: 还剩{count}次刷新", e)
count += 1
if count == scolltime :
# pyautogui.press('f5')
if platform == "linkedin":
x, y = pyautogui.locateCenterOnScreen(next_pic)
#找到图片后点击,并将计数器减1
pyautogui.click(x, y)
else:
pyautogui.press('f5')
time.sleep(delay * 2) # 等待10秒钟再继续
# 刷新次数加1
refresh_count += 1
continue
主要目的是在程序无法找到指定的图片时进行滚动页面或者刷新页面,然后重新尝试找到图片并点击。具体来说:
-
try
语句块内部尝试寻找指定的图片,如果找到了则进行点击,并将计数器减1,表示完成了一次点击任务。 -
如果
try
块中的代码出现了异常(无法找到指定的图片),则执行except
语句块中的代码。在这个except
块中,首先生成了一个随机数组my_array
,然后对这个数组进行循环遍历。 -
在循环内部,程序会随机选择一个下滚动距离,然后执行滚动操作
pyautogui.scroll(my_array[random_index])
。 -
在滚动后,程序尝试再次寻找指定的图片,如果找到了,就执行点击操作,并更新计数器,表示完成了一次点击任务。
-
如果在滚动和重新寻找图片的过程中依然无法找到指定的图片,程序会继续循环,直到达到滚动次数的限制。在达到滚动次数限制后,程序会尝试进行页面刷新(通过按下 F5 键),然后等待一段时间后继续执行循环。
-
在进行页面刷新后,会将刷新次数加1,以便在未来的循环中对刷新次数进行判断,并可能采取不同的等待策略。