selenium处理极验滑动验证码
要爬取一个网站遇到了极验的验证码,这周都在想着怎么破解这个,网上搜了好多知乎上看到有人问了这问题https://www.zhihu.com/question/28833985,我按照这思路去大概实现了一下。
1.使用htmlunit(这种方式我没成功,模拟鼠标拖拽后轨迹没生成,可以跳过)
我用的是java,我首先先想到了用直接用htmlunit,我做了点初始化
private void initWebClient() {
if (webClient != null) {
return;
}
webClient = new WebClient(BrowserVersion.FIREFOX_24);
webClient.getOptions().setProxyConfig(new ProxyConfig("127.0.0.1",8888));
webClient.getOptions().setActiveXNative(true);
webClient.getOptions().setUseInsecureSSL(true); // 配置证书
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(true);
webClient.setCssErrorHandler(new SilentCssErrorHandler());
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
CookieManager cookieManager = new CookieManager();
List<org.apache.http.cookie.Cookie> httpCookies = client.getCookies();//其方式获取的cookie
for (org.apache.http.cookie.Cookie cookie : httpCookies) {
cookieManager.addCookie(new com.gargoylesoftware.htmlunit.util.Cookie(cookie));
}
webClient.setCookieManager(cookieManager);
}
初始化代理,cookie..然后就能正常调用了
HtmlPage page = webClient.getPage("http://www.qixin.com/login");//企信宝
gePageInfor(page);
下面就是我获取图片,还原图片并且模拟拖拽,(这里我觉得是有些问题的,可能是拖拽我模拟的不对导致触发的js并没有生成正确的轨迹,还请大家帮忙看看哪里错了)
private void gePageInfor(HtmlPage page) {
String[] img_slice={"div", "class", "gt_cut_fullbg_slice"};
String[] img_bg_slice={"div", "class", "gt_cut_bg_slice"};
HtmlDivision div = (HtmlDivision) page.getElementById("captcha");
int deCAPTCHA = 0;
try {
byte[] img_slice_binary = client.get(getImgUrl(img_slice, div, true)).getBinary();//获取图片byte
byte[] img_bg_slice_binary = client.get(getImgUrl(img_bg_slice, div, false)).getBinary();
//获取还原后的图片
BufferedImage geetestImg = ImgTest.getGeetestImg(img_slice_binary, ImgTest.imgArray);
BufferedImage geetestImg2 = ImgTest.getGeetestImg(img_bg_slice_binary, ImgTest.imgArray);
//获得图片移动位置(目前还有问题,需改用第三方图片识别)
deCAPTCHA =ImgTest.deCAPTCHA(geetestImg,geetestImg2);
System.out.println(deCAPTCHA);
} catch (IOException | FetchException e) {
e.printStackTrace();
}
HtmlDivision div_slider_knob = get_div_slider_knob(page,"gt_slider_knob gt_show");//获取要移动div
HtmlPage mouseOver = (HtmlPage) div_slider_knob.mouseOver();
HtmlPage mouseDownPage = (HtmlPage)div_slider_knob.mouseDown();
div_slider_knob = get_div_slider_knob(mouseDownPage,"gt_slider_knob gt_show moving");
mouseMoveX(deCAPTCHA, div_slider_knob, mouseDownPage);
HtmlPage newPage =(HtmlPage)div_slider_knob.mouseOver();
// newPage =(HtmlPage)div_slider_knob.mouseDown();
System.out.println(newPage.asXml());
div = (HtmlDivision)newPage.getElementById("captcha");
HtmlElement htmlElement = div.getElementsByAttribute("div", "class", "gt_slice gt_show moving").get(0);
System.out.println(htmlElement);
newPage =(HtmlPage)div_slider_knob.mouseUp();//触发js,轨迹没有生成
System.out.println("---------------");
System.out.println(newPage.asXml());
if (newPage.getElementById("captcha")!=null) {//错误重试
//gePageInfor(newPage);
}
} private void mouseMoveX(int deCAPTCHA, HtmlDivision div_slider_knob, HtmlPage mouseDown) {
MouseEvent mouseEvent = new MouseEvent(div_slider_knob, MouseEvent.TYPE_MOUSE_MOVE, false, false, false, MouseEvent.BUTTON_LEFT);
mouseEvent.setClientX( mouseEvent.getClientX()+((deCAPTCHA!=0)?deCAPTCHA:99)); //移动x坐标
ScriptResult scriptResult = mouseDown.getDocumentElement().fireEvent(mouseEvent);
}
private HtmlDivision get_div_slider_knob(HtmlPage page,String classString) {
return (HtmlDivision)(((HtmlDivision) page.getElementById("captcha")).getElementsByAttribute("div", "class", classString).get(0));
} private String getImgUrl(String[] img_slice, HtmlDivision div, boolean isNeedCheckPostion) {
String url ="";
int[] postion = new int[2];
boolean empty = div.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]).isEmpty();
if (div.hasChildNodes() && !empty) {
List<HtmlElement> elementsByAttribute = div.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]);
for(int i = 0;i<elementsByAttribute.size();i++){
HtmlDivision div_img = (HtmlDivision)elementsByAttribute.get(i);
String style = div_img.getAttribute("style");
String[] imge_url_position = style.split(";");
if(StringUtils.isBlank(url)){//确认url
url = StringUtils.replacePattern(imge_url_position[0], ".*\\(", "").replace(")", "");
}
if (isNeedCheckPostion) {//确认图片切割postion,两张图切割方式一样 background-position: -157px -58px
// String[] positionS = StringUtils.split(StringUtils.remove(imge_url_position[1], "px").replace("-", "").replaceAll(".*:", ""), null);
String[] positionS = StringUtils.split(StringUtils.removePattern(imge_url_position[1], "[^\\d+ \\s]"),null);
postion[0] = Integer.parseInt(positionS[0]);
postion[1] = Integer.parseInt(positionS[1]);
int[] is = ImgTest.imgArray[i];
if (is[0]!=postion[0]||is[1]!=postion[1]) {
logger.debug("更新分割postion");
ImgTest.imgArray[i] = postion;
}
System.out.println(ImgTest.imgArray);
isNeedCheckPostion= false;
}
}
}
return url;
}
对比图片获取位移方法(deCAPTCHA)是错的我就不放代码了,下面是其中还原图片用的方法,目前是其实审查元素后你就明白怎么还原这个图片了,这里是每次读的10px,58px
public static BufferedImage getGeetestImg(byte[] binary, int[][] imgArray) throws IOException {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(binary));
List<BufferedImage> list = new ArrayList<>();
for (int i=0;i< imgArray.length;i++) {
BufferedImage subimage = img.getSubimage(imgArray[i][0], imgArray[i][1], 10, 58);
list.add(subimage);
// ImageIO.write(subimage, "jpg", new File("d:\\image\\imgs"+i+".jpg"));
}
BufferedImage mergeImageUp = null;
BufferedImage mergeImageDown = null;
int mid = list.size()>>>1;
for (int i = 0; i <mid-1 ; i++) {
mergeImageUp = mergeImage(mergeImageUp==null?list.get(i):mergeImageUp, list.get(i+1), true);
}
for(int i = mid;i<list.size()-1;i++){
mergeImageDown = mergeImage(mergeImageDown==null?list.get(i):mergeImageDown,list.get(i+1), true);
}
img = mergeImage(mergeImageUp, mergeImageDown, false);
return img;
}
public static BufferedImage mergeImage(BufferedImage img1,
BufferedImage img2, boolean isHorizontal) throws IOException {
int w1 = img1.getWidth();
int h1 = img1.getHeight();
int w2 = img2.getWidth();
int h2 = img2.getHeight();
// 从图片中读取RGB
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
int[] ImageArrayTwo = new int[w2 * h2];
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2); // 生成新图片
BufferedImage DestImage = null;
if (isHorizontal) { // 水平方向合并
DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2);
} else { // 垂直方向合并
DestImage = new BufferedImage(w1, h1 + h2,
BufferedImage.TYPE_INT_RGB);
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
} return DestImage;
}
2.使用selenium
后来我想着是我模拟鼠标这个动作哪里有问题,我就又找到了selenium(2.42.2),他也能操作htmlunit关键他的鼠标动作好像封装比较完全
但是我尝试了以后发现了这个,HtmlUnitMouse这个动作没有实现
public void mouseMove(Coordinates where, long xOffset, long yOffset) {
throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported.");
}
好吧,于是调用chrome吧
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
Proxy proxy = new Proxy();
//设置代理服务器地址
proxy.setHttpProxy("127.0.0.1:8888");
// DesiredCapabilities capabilities = DesiredCapabilities.htmlUnitWithJs();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// final WebDriver driver = new HtmlUnitDriver(capabilities);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.qixin.com/login");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
checkPage(driver,"return $('.gt_cut_fullbg_slice');");
// 获取 网页的 title
System.out.println("1 Page title is: " + driver.getTitle());
// 通过 id 找到 input 的 DOM
String pageSource = driver.getPageSource();
System.out.println(pageSource);
org.openqa.selenium.JavascriptExecutor executor = (org.openqa.selenium.JavascriptExecutor)driver;
boolean equals = executor.executeScript("return document.readyState").equals("complete");
int moveX =99;//移动位置
if (equals) {
WebElement element = driver.findElement(By.className("gt_slider_knob"));//(".gt_slider_knob"));
Point location = element.getLocation();
element.getSize();
Actions action = new Actions(driver);
// action.clickAndHold().perform();// 鼠标在当前位置点击后不释放
// action.clickAndHold(element).perform();// 鼠标在 onElement 元素的位置点击后不释放
// action.clickAndHold(element).moveByOffset(location.x+99,location.y).release().perform(); //选中source元素->拖放到(xOffset,yOffset)位置->释放左键
action.dragAndDropBy(element, location.x+moveX,location.y).perform();
// action.dragAndDrop(element,newelement).perform();
pageSource = driver.getPageSource();
}
//更新cookie
Set<org.openqa.selenium.Cookie> cookies = driver.manage().getCookies();
Set<Cookie> cookies2 = new HashSet<>();
for (org.openqa.selenium.Cookie cookie : cookies) {
cookies2.add((Cookie) new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), true));
}
for (Cookie cookie : cookies2) {
org.apache.http.cookie.Cookie httpClient = cookie.toHttpClient();
}
System.out.println(pageSource);
这样提交的表单确实是有轨迹的,这里移动位置我先写了个固定值,可以由上面图片还原,以及一些开源的图片识别工具识别出位置。以上应该就能解决这个滑动验证码了
selenium处理极验滑动验证码的更多相关文章
- selenium+java破解极验滑动验证码的示例代码
转自: https://www.jianshu.com/p/1466f1ba3275 selenium+java破解极验滑动验证码 卧颜沉默 关注 2017.08.15 20:07* 字数 3085 ...
- Python——破解极验滑动验证码
极验滑动验证码 以上图片是最典型的要属于极验滑动认证了,极验官网:http://www.geetest.com/. 现在极验验证码已经更新到了 3.0 版本,截至 2017 年 7 月全球已有十六万家 ...
- python验证码识别(2)极验滑动验证码识别
目录 一:极验滑动验证码简介 二:极验滑动验证码识别思路 三:极验验证码识别 一:极验滑动验证码简介 近些年来出现了一些新型验证码,不想旧的验证码对人类不友好,但是这种验证码对于代码来说识别难度上 ...
- Python 破解极验滑动验证码
Python 破解极验滑动验证码 测试开发社区 1周前 阅读目录 极验滑动验证码 实现 位移移动需要的基础知识 对比两张图片,找出缺口 获得图片 按照位移移动 详细代码 回到顶部 极验滑动验证码 以 ...
- thinkphp整合系列之极验滑动验证码
对于建站的筒子们来说:垃圾广告真是让人深恶痛绝:为了清净:搞个难以识别的验证码吧:又被用户各种吐槽:直到后来出现了极验这个滑动的验证码:这真是一个体验好安全高的方案:官网:http://www.gee ...
- vue_drf之实现极验滑动验证码
一.需求 1,场景 我们在很多登录和注册场景里,为了避免某些恶意攻击程序,我们会添加一些验证码,也就是行为验证,让我们相信现在是一个人在交互,而不是一段爬虫程序.现在市面上用的比较多的,比较流行的是极 ...
- selenium+java破解极验滑动验证码
摘要 分析验证码素材图片混淆原理,并采用selenium模拟人拖动滑块过程,进而破解验证码. 人工验证的过程 打开威锋网注册页面(https://passport.feng.com/?r=user/r ...
- luffy之多条件登录与极验滑动验证码
多条件登录 JWT扩展的登录视图,在收到用户名与密码时,也是调用Django的认证系统中提供的authenticate()来检查用户名与密码是否正确. 我们可以通过修改Django认证系统的认证后端( ...
- thinkphp整合系列之极验滑动验证码geetest
给一个央企做官网,登录模块用的thinkphp验证码类.但是2019-6-10到12号,国家要求央企检验官网漏洞,防止黑客攻击,正直贸易战激烈升级时期,所以各事业单位很重视官网安全性,于是乎集团总部就 ...
随机推荐
- C++ 一些笔记
1.指针.引用和const ; int &a = i; int *p ; int *&r=p;//r是对指针的引用 r=&i;//实质上是将p指向i *r=;//实质上是将i的 ...
- PP读取生产订单状态
转自http://blog.csdn.net/heng0757/article/details/6656089 传递订单号码,可以返回该订单的状态 REPORT ZCMH2.TABLES : JES ...
- li标签的点击范围
<div class="login_menu_choose"> <ul class="cursor"> & ...
- [非原创]Project facet Java version 1.8 is not supported解决记录
原博地址:http://blog.csdn.net/dingchenxixi/article/details/51496998 一看知道是因为jdk版本不一致所导致,如何解决? 方法一: 选中项目 P ...
- Swift建立栈的泛型结构体以及top()、push()、pop()定义函数的定义
首先可以使用swift定义Stack的结构体 //泛型表达 struct Stack<T> { var items = <T>() //定义栈顶函数,返回栈顶元素 mutati ...
- Android之ScrollView嵌套ListView和GridView冲突
由于ListView,GridView本身都继承于ScrollView,一旦在ScrollView中嵌套ScrollView, 在ScrollView中嵌套使用ListView或者GridView,L ...
- EF Core CodeFirst实践 ( 使用MS SqlServer)
这里使用 MS SQLSERVER ,网上大多使用 SQLite 先来一个CodeFirst 新建项目 这里我们选择 ASP.NET Core Web Application (.NET Core) ...
- java中文乱码分析整理
在JavaWeb应用开发中,经常会出现页面中本该显示中文的地方却是乱码的情况.究其原因,主要是由于在Web组件之间.或Web组件与浏览器.与数据库所使用的字符集标准不统一,Web应用程序运行过程中,中 ...
- java批量insert入mysql数据库
mysql 批量insert语句为 insert into Table_(col1,col2...) values(val11,val12...),(val11,val12...),...; java ...
- System.DateUtils 1. DateOf、TimeOf 简单修饰功能
编译版本:Delphi XE7 { Simple trimming functions } // 简单修饰功能 function DateOf(const AValue: TDateTime): TD ...