绕过登录模块的腾讯云滑块验证码
链接:http://testerhome.com/topics/32507
import random
import io
from PIL import Image
from urllib.request import urlopen
from selenium.webdriver.common.action_chains import ActionChains
import time
# 打开图片链接
def open_img_url(src):
img_url = urlopen(src, timeout=2).read()
img = Image.open(io.BytesIO(img_url))
# 恢复原始大小
img_resize = (int(i // 2) for i in img.size)
return img.resize(img_resize, Image.ANTIALIAS)
def get_img(dr):
dr.switch_to.frame('tcaptcha_iframe')
src1 = dr.find_element_by_id('slideBg').get_attribute('src')
src2 = src1.replace('img_index=1', 'img_index=0')
return open_img_url(src1), open_img_url(src2)
# 比较两张图片同一点上的像数值,差距大于设置标准返回False
def is_pixel(img1, img2, x, y):
i = 100
px1, px2 = img1.load()[x, y], img2.load()[x, y]
r, g, b = [abs(p1 - p2) for p1, p2 in zip(px1, px2)]
return True if r < i and g < i and b < i else False
# 获取缺口的偏移量,两张图片对比,(i,j)像素点的RGB差距,过大则该x为偏移值
def get_offset(img1, img2):
offset, distance = None, 70
x, y = img1.size
for i in range(distance, x):
for j in range(y):
if not is_pixel(img1, img2, i, j):
offset = i
return offset
return offset
# 计算滑块的移动轨迹 滑块并不是从0开始移动,有一个初始值
def get_track(offset):
offset -= 30
return [offset / 4] * 4
# 模拟释放鼠标抖动
def shake(dr):
ActionChains(dr).move_by_offset(xoffset=-2, yoffset=0).perform()
ActionChains(dr).move_by_offset(xoffset=2, yoffset=0).perform()
time.sleep(random.random())
# 按轨迹移动
def move(dr, track):
for i in track:
ActionChains(dr).move_by_offset(xoffset=i, yoffset=0).perform()
time.sleep(random.random() / 100)
time.sleep(random.random())
# 完成拖动操作
def slider_btn(dr, track):
btn = dr.find_element_by_id('tcaptcha_drag_thumb')
ActionChains(dr).click_and_hold(btn).perform() # 按住按钮不放
move(dr, track) # 按正向轨迹移动
move(dr, [-1, -0.5, -1]) # 按逆向轨迹移动
shake(dr) # 模拟人手抖动
ActionChains(dr).release().perform() # 松开滑块按钮
# 处理验证码
def deal_slider_captcha(dr):
time.sleep(3)
img1, img2 = get_img(dr)
offset = get_offset(img1, img2)
track = get_track(offset)
slider_btn(dr, track)
#基于 selenium 的 webdriver
from selenium import webdriver
if __name__ == '__main__':
dr = webdriver.Chrome('chromedriver的地址')
deal_slider_captcha(dr)