爬虫: 滑块验证码破解
以下代码应用于头条号登录获取cookie,99%通过率。希望头条号能够提供授权发表文章的API。
第一步,计算缺口距离
先对背景图与缺口图发请求,保存为本地文件,然后使用函数FindPic计算出缺口距离
def FindPic(target, template):
"""
找出图像中最佳匹配位置
:param target: 目标即背景图
:param template: 模板即需要找到的图
:return: 返回最佳匹配及其最差匹配和对应的坐标
"""
target_rgb = cv2.imread(target)
target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
template_rgb = cv2.imread(template, 0)
res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
value = cv2.minMaxLoc(res)
return value[2:][0][0], value[2:][1][0] # 采用value[2:][1][0]
第二步,生成加减速为主的滑动轨迹
def generate_tracks(distance):
distance += 20
v = 0
t = 0.2
forward_tracks = []
current = 0
mid = distance * 3 / 5 # 减速阀值
while current < distance:
if current < mid:
a = 2 # 加速度为+2
else:
a = -3 # 加速度-3
s = v * t + 0.5 * a * (t ** 2)
v = v + a * t
current += s
forward_tracks.append(round(s))
back_tracks = [-3, -3, -2, -2, -2, -2, -2, -1, -1, -1]
return forward_tracks, back_tracks
第三步,使用selenium实现滑动
value_1, value_2 = findpic(target_name, template_name)
action = ActionChains(driver) # 实例化一个action对象
button = driver.find_element_by_xpath('//*[@id="validate-drag-wrapper"]/div[2]/img') # 找到滑块
try:
action.click_and_hold(button).perform() # perform()用来执行ActionChains中存储的行为
except StaleElementReferenceException as e:
pass
action.reset_actions()
forward_tracks, back_tracks = generate_tracks(value_2)
for x in forward_tracks:
action.move_by_offset(x, 0) # 前进移动滑块
for x in back_tracks:
action.move_by_offset(x, 0) # 后退移动滑块
action.release().perform() # 松开鼠标