Teach you python to crack the slider verification code! Remember to favorite!


foreword

Some friends reported in private messages that in the process of web automation, they are often stuck by the login verification code, and they don’t know how to pass the verification code verification. Today I will talk to you about the problem of verification codes. In general, we can ask developers to help solve the verification codes, and close the verification codes! Do we have a way to deal with these verification codes? The answer is of course yes. Common verification codes are generally divided into two categories, one is graphic verification codes, and the other is slider verification codes!

insert image description here
Regarding the verification code for image-text recognition, related recognition solutions have been released before, so I won’t introduce too much today. Interested partners can get the supporting video materials . Today we mainly talk about how to identify and crack the sliding verification code.

→→→Get the information first, and then learn←←←

1. Sliding verification cracking ideas

The idea of ​​cracking the sliding verification code is generally the following two steps:

  1. Get the sliding distance of the slider

  2. Simulate dragging the slider and pass the verification.

It looks difficult, but it is actually not easy at all. But to get the sliding distance of the slider, most friends have no idea and don't know how to get it. In fact, it is not difficult to obtain it. Regarding this kind of sliding verification code, the slider and the background of the gap are each an independent picture. We can download these two pictures and use image recognition technology to identify them. The position of the gap in the background image, and then subtract the current position of the slider to get the distance to slide. At this time, many friends will think that I don’t know how to use image recognition technology, it doesn’t matter, I will give you a packaged slider recognition module later, as long as you pass in the element nodes of the slider and the gap background image, it can be calculated The notch position of the slider.

2. Case explanation

Not much to say, let's look at a case first. Here I used a sliding distance recognition module slideVerfication packaged by myself. Friends who need it can get it from the business card at the end of the article. The implementation steps of the login case are as follows:

  1. Create a driver object and access the qq login page
  2. Enter account password
  3. Click to Login
  4. Simulate sliding verification

3. Code implementation

import time
from selenium import webdriver
from slideVerfication import SlideVerificationCode
​
# 1、创建一个driver对象,访问qq登录页面
browser = webdriver.Chrome()
browser.get("https://qzone.qq.com/")
​
# 2、输入账号密码
# 2.0 点击切换到登录的iframe
browser.switch_to.frame('login_frame')
# 2.1 点击账号密码登录
browser.find_element_by_id('switcher_plogin').click()
# 2.2定位账号输入框,输入账号
browser.find_element_by_id("u").send_keys("123292678")
# 2.3定位密码输入输入密码
browser.find_element_by_id("p").send_keys("PYTHON01")
# 3、点击登录
browser.find_element_by_id('login_button').click()
time.sleep(3)
​
# 4、模拟滑动验证
# 4.1切换到滑动验证码的iframe中
tcaptcha = browser.find_element_by_id("tcaptcha_iframe")
browser.switch_to.frame(tcaptcha)
# 4.2 获取滑动相关的元素
# 选择拖动滑块的节点
slide_element = browser.find_element_by_id('tcaptcha_drag_thumb')
# 获取滑块图片的节点
slideBlock_ele = browser.find_element_by_id('slideBlock')
# 获取缺口背景图片节点
slideBg = browser.find_element_by_id('slideBg')
# 4.3计算滑动距离
sc = SlideVerificationCode(save_image=True)
distance = sc.get_element_slide_distance(slideBlock_ele,slideBg)
# 滑动距离误差校正,滑动距离*图片在网页上显示的缩放比-滑块相对的初始位置
distance = distance*(280/680) - 22
print("校正后的滑动距离",distance)
# 4.4、进行滑动
sc.slide_verification(browser,slide_element,distance=100)

insert image description here
The identification problem of the sliding verification code is solved in this way. Next, let’s talk about the identification principle of the packaged slideVerfication module. In fact, the image recognition of this module is also recognized with the help of a third-party image processing module. There are many ready-made libraries in python for processing images. Here I use opencv-python for recognition. Some reference codes of the two methods used in the slideVerfication module are as follows:

def get_element_slide_distance(self, slider_ele, background_ele, correct=0):

According to the incoming slider and the background node, calculate the distance of the slider. This method can only calculate the scene where the slider and the background image are a complete picture. If the background image is a background image spliced ​​​​by multiple small images, this method Not applicable, please use the get_image_slide_distance method

:param slider_ele: 滑块图片的节点
:type slider_ele: WebElement
:param background_ele: 背景图的节点
:type background_ele:WebElement
:param correct:滑块缺口截图的修正值,默认为0,调试截图是否正确的情况下才会用
:type: int
:return: 背景图缺口位置的X轴坐标位置(缺口图片左边界位置)
# 获取验证码的图片
slider_url = slider_ele.get_attribute("src")
background_url = background_ele.get_attribute("src")
# 下载验证码背景图,滑动图片
slider = "slider.jpg"
background = "background.jpg"
self.onload_save_img(slider_url, slider)
self.onload_save_img(background_url, background)
# 读取进行色度图片,转换为numpy中的数组类型数据,
slider_pic = cv2.imread(slider, 0)
background_pic = cv2.imread(background, 0)
# 获取缺口图数组的形状 -->缺口图的宽和高
width, height = slider_pic.shape[::-1]
# 将处理之后的图片另存
slider01 = "slider01.jpg"
background_01 = "background01.jpg"
cv2.imwrite(background_01, background_pic)
cv2.imwrite(slider01, slider_pic)
# 读取另存的滑块图
slider_pic = cv2.imread(slider01)
# 进行色彩转换
slider_pic = cv2.cvtColor(slider_pic, cv2.COLOR_BGR2GRAY)
# 获取色差的绝对值
slider_pic = abs(255 - slider_pic)
# 保存图片
cv2.imwrite(slider01, slider_pic)
# 读取滑块
slider_pic = cv2.imread(slider01)
# 读取背景图
background_pic = cv2.imread(background_01)
# 比较两张图的重叠区域
result = cv2.matchTemplate(slider_pic, background_pic, cv2.TM_CCOEFF_NORMED)
# 获取图片的缺口位置
top, left = np.unravel_index(result.argmax(), result.shape)
# 背景图中的图片缺口坐标位置
print("当前滑块的缺口位置:", (left, top, left + width, top + height))
return left
def slide_verification(self, driver, slide_element, distance):

Swipe the slider to verify

:param driver: driver对象
:type driver:webdriver.Chrome
:param slide_element: 滑块的元组
:type slider_ele: WebElement
:param distance: 滑动的距离
:type: int
:return:
# 获取滑动前页面的url地址
start_url = driver.current_url
print("需要滑动的距离为:", distance)
# 根据滑动距离生成滑动轨迹
locus = self.get_slide_locus(distance)
print("生成的滑动轨迹为:{},轨迹的距离之和为{}".format(locus, distance))
# 按下鼠标左键
ActionChains(driver).click_and_hold(slide_element).perform()
time.sleep(0.5)
# 遍历轨迹进行滑动
for loc in locus:
time.sleep(0.01)
ActionChains(driver).move_by_offset(loc, random.randint(-5, 5)).perform()
ActionChains(driver).context_click(slide_element)
# 释放鼠标
ActionChains(driver).release(on_element=slide_element).perform()

Well, I will share with you about the sliding verification code recognition here. The above solutions also have corresponding explanation videos. If you need it, get the business card below!

↓ ↓ ↓ Add the business card below to find me, directly get the source code and cases ↓ ↓ ↓

Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_45841831/article/details/131544263