滑動驗證碼驗證如下圖所示:
解決這個問題的思路:
1、獲取無缺口和有缺口的兩張圖片,即:下圖所示:
2、對比兩張圖片的像素點,像素點差值超過一定值即可視爲缺口點(這裏對比像素點時最好將開始的那一部分截取出來不對比,因爲滑塊滑動的距離肯定不會爲0)。
3、找到缺口之後計算活動距離,並模擬人工滑動滑塊。
有了這個思路之後貼一下核心代碼:
截取圖片(首先需要定位到圖片的位置並獲取size):
screenshot = driver.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
captcha1 = screenshot.crop((left, top, right, bottom))
captcha1.save('captcha1.png')
獲取偏移量,也就是缺口的位置(這裏得出的偏移量可能會有誤差,因此其值的大小需要多試驗幾次去校準):
left = 55 # 這個是去掉開始的一部分
for i in range(left, captcha1.size[0]):
for j in range(captcha1.size[1]):
# 判斷兩個像素點是否相同
pixel1 = captcha1.load()[i, j]
pixel2 = captcha2.load()[i, j]
threshold = 60
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(pixel1[2] - pixel2[2]) < threshold:
pass
else:
left = i
模擬人工滑動(先加速後減速,先滑過在滑回來。滑動若是勻速、快速、精準的話那都會被判定爲是機器操作):
# 移動軌跡
track = []
# 當前位移
current = 0
# 減速閾值
mid = distance * 2 / 5
# 計算間隔
t = 0.2
# 初速度
v = 1
while current < distance:
if current < mid:
# 加速度爲正2
a = 5
else:
# 加速度爲負3
a = -2
# 初速度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))
track += [5, -5] # 滑過去再滑過來,不然有可能被喫
有了滑動的速度只有再用selenium點擊滑動就OK了,最後別忘記加一個驗證通過的代碼,如果沒通過記得刪除cookie重來一遍。
這個驗證識別是參考了崔老師的書上的內容並加了一下自己的理解的思路,有需要完整代碼的童鞋可以聯繫我。
完整代碼(有用的小夥伴記得點贊關注呀):
# coding=utf-8
from selenium import webdriver
import time
from PIL import Image,ImageGrab
from io import BytesIO
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import tesserocr
'''
用於天眼查自動登錄,解決滑塊驗證問題
'''
def get_track(distance):
"""
根據偏移量獲取移動軌跡
:param distance: 偏移量
:return: 移動軌跡
"""
# 移動軌跡
track = []
# 當前位移
current = 0
# 減速閾值
mid = distance * 2 / 5
# 計算間隔
t = 0.2
# 初速度
v = 1
while current < distance:
if current < mid:
# 加速度爲正2
a = 5
else:
# 加速度爲負3
a = -2
# 初速度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 autologin(account, password):
driver.get('https://www.tianyancha.com/?jsid=SEM-BAIDU-PP-SY-000873&bd_vid=7864822754227867779')
time.sleep(3)
try:
driver.find_element_by_xpath('//*[@id="tyc_banner_close"]').click()
except:
pass
driver.find_element_by_xpath('//*[@id="web-content"]/div/div[1]/div[1]/div/div/div[2]/div/div[4]/a').click()
time.sleep(3)
# 這裏點擊密碼登錄時用id去xpath定位是不行的,因爲這裏的id是動態變化的,所以這裏換成了class定位
driver.find_element_by_xpath(
'.//div[@class="modal-dialog -login-box animated"]/div/div[2]/div/div/div[3]/div[1]/div[2]').click()
time.sleep(3)
accxp = './/div[@class="modal-dialog -login-box animated"]/div/div[2]/div/div/div[3]/div[2]/div[2]/input'
pasxp = './/div[@class="modal-dialog -login-box animated"]/div/div[2]/div/div/div[3]/div[2]/div[3]/input'
driver.find_element_by_xpath(accxp).send_keys(account)
driver.find_element_by_xpath(pasxp).send_keys(password)
clixp = './/div[@class="modal-dialog -login-box animated"]/div/div[2]/div/div/div[3]/div[2]/div[5]'
driver.find_element_by_xpath(clixp).click()
# 點擊登錄之後開始截取驗證碼圖片
time.sleep(2)
img = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
time.sleep(0.5)
# 獲取圖片位子和寬高
location = img.location
size = img.size
# 返回左上角和右下角的座標來截取圖片
top,bottom,left,right = location['y'], location['y']+size['height'], location['x'], location['x']+size['width']
# 截取第一張圖片(無缺口的)
screenshot = driver.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
captcha1 = screenshot.crop((left, top, right, bottom))
print('--->', captcha1.size)
captcha1.save('captcha1.png')
# 截取第二張圖片(有缺口的)
driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]').click()
time.sleep(4)
img1 = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[1]/div[2]/div[1]')
time.sleep(0.5)
location1 = img1.location
size1 = img1.size
top1,bottom1,left1,right1 = location1['y'], location1['y']+size1['height'], location1['x'], location1['x']+size1['width']
screenshot = driver.get_screenshot_as_png()
screenshot = Image.open(BytesIO(screenshot))
captcha2 = screenshot.crop((left1, top1, right1, bottom1))
captcha2.save('captcha2.png')
# 獲取偏移量
left = 55 # 這個是去掉開始的一部分
for i in range(left, captcha1.size[0]):
for j in range(captcha1.size[1]):
# 判斷兩個像素點是否相同
pixel1 = captcha1.load()[i, j]
pixel2 = captcha2.load()[i, j]
threshold = 60
if abs(pixel1[0] - pixel2[0]) < threshold and abs(pixel1[1] - pixel2[1]) < threshold and abs(
pixel1[2] - pixel2[2]) < threshold:
pass
else:
left = i
print('缺口位置', left)
# 減去缺口位移
left -= 54
# 開始移動
track = get_track(left)
print('滑動軌跡', track)
track += [5, -5, 2, -2] # 滑過去再滑過來,不然有可能被喫
# 拖動滑塊
slider = driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]')
ActionChains(driver).click_and_hold(slider).perform()
for x in track:
ActionChains(driver).move_by_offset(xoffset=x, yoffset=0).perform()
ActionChains(driver).release().perform()
time.sleep(2)
try:
if driver.find_element_by_xpath('/html/body/div[10]/div[2]/div[2]/div[2]/div[2]'):
print('能找到滑塊,重新試')
driver.delete_all_cookies()
driver.refresh()
autologin(driver, account, password)
else:
print('login success')
except:
print('login success')
# if __name__ == '__main__':
driver_path = 'C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe'
# chromeoption = webdriver.ChromeOptions()
# chromeoption.add_argument('--headless')
# chromeoption.add_argument('user-agent='+user_agent)
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(10)
account = 'xxxx'
password = 'xxxx'
autologin(account, password)