目录javascript
近些年来出现了一些新型验证码,不想旧的验证码对人类不友好,可是这种验证码对于代码来讲识别难度上升了几个等级。所以须要其余的手段进行处理。
识别须要的python库:selenium和ChromeDriver驱动,不一样浏览器的要下载的驱动库不一样。
验证码获取网站:http://www.geetest.com/
极验滑动验证码已经到了3.0版本,相关于图形验证码识别难度更大,原理是拖动图片到缺口处,而后拼合图像进行验证,会生成三个加密参数,经过表单提交到后台,后台再进行验证。
极验验证码还增长了机器学习的方法来识别是不是恶意程序进行识别,有防模拟,防伪造,防暴力的方式, 只需0.4秒,而且保护资源不被滥用和盗取。
咱们的程序通常只要不是恶意进行爬取的,并遵照爬虫协议,就能够。千万不要给服务器形成负担。java
这里咱们能够采用模拟浏览器动做的方式完成验证,用Selenium来彻底模拟人的行为完成验证。
主要分为三步
(1)模拟点击验证按钮
(2)识别滑动缺口的位置
(3)模拟拖动滑块
第(1)步还比较好说,第(2)步操做识别接口的位置比较关键,须要用到图像处理看到接口的位置,并和原图对比检测的方法来识别缺口的位置。同时获取两张图片,设定一个对比阈值,而后遍历两张图片,找出相同像素RGB差距超过此阈值的像素点,那么像素点位置就是缺口的位置。
第(3)步较难,因为人的移动轨迹是先加速后减速,匀速移动和随机移动等方法都不能经过验证,要模拟好这个过程。python
官网图片为:
web
# 注册的用户名和密码 email = '' password = '' class CrackGeetest(): def __init__(self): self.url = 'https://account.geetest.com/login' self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser, 20) self.email = email self.password = password
识别验证码第一步就是模拟点击初始的验证按钮,用显式等待的方法进行获取。canvas
def get_geetest_button(self): """ 获取初始验证按钮 返回值:按钮对象 """ button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip'))) return button
在调用位置便可模拟点击:浏览器
# 点击验证按钮 button = self.get_geetest_button() button.click()
接下来识别缺口的位置,首先获取两张图片,进行对比,不同的位置就是缺口。
获取不带缺口的图片。用selenium选取图片元素获得整个网页的截图而后裁剪便可,代码以下:服务器
def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_position(self): """ 获取验证码位置 :return: 验证码位置元组 """ img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img'))) time.sleep(2) location = img.location size = img.size top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[ 'width'] return (top, bottom, left, right) def get_geetest_image(self, name='captcha.png'): """ 获取验证码图片 :return: 图片对象 """ top, bottom, left, right = self.get_position() print('验证码位置', top, bottom, left, right) screenshot = self.get_screenshot() captcha = screenshot.crop((left, top, right, bottom)) captcha.save(name) return captcha
接下来须要获取第二张图片,就是带有缺口的图片,只须要点击下面的滑块就能出现缺口,代码以下:app
def get_slider(self): """ 获取滑块 :return: 滑块对象 """ slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button'))) return slider
用click()便可触发点击,以下:机器学习
# 点按呼出缺口 slider = self.get_slider() slider.click()
接下来就是经过对比图片获取缺口,经过遍历图片上的每一个坐标点,获取两张图片对应像素点的RGB数据。若是在必定范围内,那就表明两个像素相同,继续对比下一个像素点。若是差距超过必定范围,则表明像素点不一样,当前位置就是缺口位置。经过设置一个阈值threshold,来进行判断,代码以下:ide
def is_pixel_equal(self, image1, image2, x, y): """ 判断两个像素是否相同 :param image1: 图片1 :param image2: 图片2 :param x: 位置x :param y: 位置y :return: 像素是否相同 """ # 取两个图片的像素点 pixel1 = image1.load()[x, y] pixel2 = image2.load()[x, y] threshold = 60 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs( pixel1[2] - pixel2[2]) < threshold: return True else: return False def get_gap(self, image1, image2): """ 获取缺口偏移量 :param image1: 不带缺口图片 :param image2: 带缺口图片 :return: """ left = 60 for i in range(left, image1.size[0]): for j in range(image1.size[1]): if not self.is_pixel_equal(image1, image2, i, j): left = i return left return left
模拟拖动并不复杂,可是里面的细节比较多。用相关的函数将滑块拖动到对应的位置便可。可是要是匀速拖动,会必然识别出是程序,非人类操做,由于人类没法作到彻底匀速拖动,会识别出是机器操做,使得验证码失败。
经过不一样的方法检测,咱们发现把前段滑块作匀加速运动,后段滑块作匀减速运动,便可完成验证。
这里加速度用a来表示,当前速度用v表示,初速度用vo表示,位移用x表示,时间用t表示。
代码以下:
def get_track(self, distance): """ 根据偏移量获取移动轨迹 :param distance: 偏移量 :return: 移动轨迹 """ # 移动轨迹 track = [] # 当前位移 current = 0 # 减速阈值 mid = distance * 4 / 5 # 计算间隔 t = 0.2 # 初速度 v = 0 while current < distance: if current < mid: # 加速度为正2 a = 2 else: # 加速度为负3 a = -3 # 初速度v0 v0 = v # 当前速度v = v0 + at v = v0 + a * t # 移动距离x = v0t + 1/2 * a * t^2 move = v0 * t + 1 / 2 * a * t * t # 当前位移 current += move # 加入轨迹 track.append(round(move)) return track def move_to_gap(self, slider, track): """ 拖动滑块到缺口处 :param slider: 滑块 :param track: 轨迹 :return: """ ActionChains(self.browser).click_and_hold(slider).perform() for x in track: ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(0.5) ActionChains(self.browser).release().perform()
import time from io import BytesIO from PIL import Image from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC EMAIL = 'cqc@cuiqingcai.com' PASSWORD = '' BORDER = 6 INIT_LEFT = 60 # 注册的用户名和密码 email = '' password = '' class CrackGeetest(): def __init__(self): self.url = 'https://account.geetest.com/login' self.browser = webdriver.Chrome() self.wait = WebDriverWait(self.browser, 20) self.email = email self.password = password def __del__(self): self.browser.close() def get_geetest_button(self): """ 获取初始验证按钮 返回值:按钮对象 """ button = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_radar_tip'))) return button def get_screenshot(self): """ 获取网页截图 :return: 截图对象 """ screenshot = self.browser.get_screenshot_as_png() screenshot = Image.open(BytesIO(screenshot)) return screenshot def get_position(self): """ 获取验证码位置 :return: 验证码位置元组 """ img = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_img'))) time.sleep(2) location = img.location size = img.size top, bottom, left, right = location['y'], location['y'] + size['height'], location['x'], location['x'] + size[ 'width'] return (top, bottom, left, right) def get_geetest_image(self, name='captcha.png'): """ 获取验证码图片 :return: 图片对象 """ top, bottom, left, right = self.get_position() print('验证码位置', top, bottom, left, right) screenshot = self.get_screenshot() captcha = screenshot.crop((left, top, right, bottom)) captcha.save(name) return captcha def get_slider(self): """ 获取滑块 :return: 滑块对象 """ slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'geetest_slider_button'))) return slider def open(self): """ 打开网页输入用户名密码 :return: None """ self.browser.get(self.url) email = self.wait.until(EC.presence_of_element_located((By.ID, 'email'))) password = self.wait.until(EC.presence_of_element_located((By.ID, 'password'))) email.send_keys(self.email) password.send_keys(self.password) def is_pixel_equal(self, image1, image2, x, y): """ 判断两个像素是否相同 :param image1: 图片1 :param image2: 图片2 :param x: 位置x :param y: 位置y :return: 像素是否相同 """ # 取两个图片的像素点 pixel1 = image1.load()[x, y] pixel2 = image2.load()[x, y] threshold = 60 if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs( pixel1[2] - pixel2[2]) < threshold: return True else: return False def get_gap(self, image1, image2): """ 获取缺口偏移量 :param image1: 不带缺口图片 :param image2: 带缺口图片 :return: """ left = 60 for i in range(left, image1.size[0]): for j in range(image1.size[1]): if not self.is_pixel_equal(image1, image2, i, j): left = i return left return left def get_track(self, distance): """ 根据偏移量获取移动轨迹 :param distance: 偏移量 :return: 移动轨迹 """ # 移动轨迹 track = [] # 当前位移 current = 0 # 减速阈值 mid = distance * 4 / 5 # 计算间隔 t = 0.2 # 初速度 v = 0 while current < distance: if current < mid: # 加速度为正2 a = 2 else: # 加速度为负3 a = -3 # 初速度v0 v0 = v # 当前速度v = v0 + at v = v0 + a * t # 移动距离x = v0t + 1/2 * a * t^2 move = v0 * t + 1 / 2 * a * t * t # 当前位移 current += move # 加入轨迹 track.append(round(move)) return track def move_to_gap(self, slider, track): """ 拖动滑块到缺口处 :param slider: 滑块 :param track: 轨迹 :return: """ ActionChains(self.browser).click_and_hold(slider).perform() for x in track: ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform() time.sleep(0.5) ActionChains(self.browser).release().perform() def login(self): """ 登陆 :return: None """ submit = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'login-btn'))) submit.click() time.sleep(10) print('登陆成功') def crack(self): # 输入用户名密码 self.open() # 点击验证按钮 button = self.get_geetest_button() button.click() # 获取验证码图片 image1 = self.get_geetest_image('captcha1.png') # 点按呼出缺口 slider = self.get_slider() slider.click() # 获取带缺口的验证码图片 image2 = self.get_geetest_image('captcha2.png') # 获取缺口位置 gap = self.get_gap(image1, image2) print('缺口位置', gap) # 减去缺口位移 gap -= BORDER # 获取移动轨迹 track = self.get_track(gap) print('滑动轨迹', track) # 拖动滑块 self.move_to_gap(slider, track) success = self.wait.until( EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功')) print(success) # 失败后重试 if not success: self.crack() else: self.login() if __name__ == '__main__': crack = CrackGeetest() crack.crack()
这种方法对于不一样的极验滑动验证码来讲都适用,关键在于识别的思路,如何识别缺口位置,如何生成运动轨迹等。以后遇到相似的验证码,均可以这样进行识别。