本文涉及内容
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from PIL import ImageChops
from PIL import Image
from io import BytesIO
import time
其他库不再介绍,可看上方链接
selenium库安装方式:pip install selenium
如图
该图片是通过canvas生成,处理的思路:
①定位图片的位置,使用screenshot_as_png截图
②使用click_and_hold()点击滑块,获取缺口的图片并截图
③通过PIL.ImageChops.difference(image_b, image_a).getbbox()获取差异坐标(缺口)
④使用move_by_offset(x,0),移动滑块通过验证
实现:
为保证截取完整的验证码图片,页面需要下拉
(document.body.scrollHeight 下拉至页面底部)
也可以直接指定下拉距离window.scrollTo(0,500)
定位图片和滑块位置
为防止滑块本身的影响对图片对比的影响,
第二张图片的滑块要隐藏起来后截取
图片对比,获取坐标
在py 3.x版本中使用ImageChops.difference()方法
需要对图片先进行“RGB”真彩处理,py 2.x版本不需要
(原因不明)
返回的是左上角和右下角的两个坐标
需要移动的距离是211减去滑块left的值10
源码:
优快云-/滑动拼图反爬.py at 2616593e598d440b8856b31b1775d9fc1b0decc7 · thinkloading/优快云- · GitHub