在做爬虫项目的时候,有时候会遇到验证码的问题,由于某些网站的验证码是动态生成的,即使是同一个链接,在不同的时间访问可能产生不同的验证码,

一 刚开始的思路就是打开这个验证码的链接,然后通过java代码get请求保存验证码图片到本地,然后用打码工具解析验证码,将验证码自动输入验证框就

可以把验证码的问题解决了,但是问题来,每次的请求同一个地址,产生的验证码图片是不一样的,所以这种方法行不通。所以只能将图片先用selenium  WebDriver

截取到本地,然后用打码工具解析ok ,自动填写验证,很好把验证码的问题解决了。

package com.entrym.main;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set; import javax.imageio.ImageIO; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; import com.entrym.crawler.util.verifyCode.Captcha;
import com.entrym.crawler.util.verifyCode.DamaUtil;
import com.entrym.domain.SogouInfo;
import com.entrym.domain.Wxinfo;
import com.entrym.util.ConfigUtil;
import com.entrym.util.DateUtil;
import com.entrym.util.HttpUtils;
import com.google.gson.Gson;
import com.vdurmont.emoji.EmojiParser; public class WebTest { private static final String GET_TITLE="/titles/getxiaoshuo";
private static final String PATH=new File("config/config.properties").getAbsolutePath();
private static final String CHROME_HOME=new File("config/chromedriver.exe").getAbsolutePath();
private static final String CHROME_HOME_LINUX=new File("config/chromedriver").getAbsolutePath();
private static final String BASEURL=ConfigUtil.reads(PATH, "baseurl"); public static void main(String[] args) throws IOException { WebDriver driver=null;
// System.setProperty("webdriver.gecko.driver", FIREFOX_HOME);
System.out.println(PATH);
String osname=System.getProperty("os.name").toLowerCase();
if(osname.indexOf("linux")>=0){
System.setProperty("webdriver.chrome.driver", CHROME_HOME_LINUX);
// driver = new MarionetteDriver();
}else{
System.setProperty("webdriver.chrome.driver", CHROME_HOME);
// driver = new MarionetteDriver();
} driver=new ChromeDriver();
driver.get("http://weixin.sogou.com/antispider/?from=%2fweixin%3Ftype%3d2%26query%3dz+%26ie%3dutf8%26s_from%3dinput%26_sug_%3dy%26_sug_type_%3d");
WebElement ele = driver.findElement(By.id("seccodeImage")); // Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot); // Get the location of element on the page
Point point = ele.getLocation(); // Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight(); // Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot); // Copy the element screenshot to disk
File screenshotLocation = new File("D:/captcha/test.png");
FileUtils.copyFile(screenshot, screenshotLocation);
WebElement classelement = driver.findElement(By.className("p2"));
String errorText=classelement.getText();
System.out.println("输出的内容是"+classelement.getText());
if(errorText.indexOf("用户您好,您的访问过于频繁,为确认本次访问为正常用户行为")>=0){
System.out.println("*********************");
DamaUtil util=new DamaUtil();
System.out.println("===================");
String code=""; //验证码
Captcha captcha=new Captcha();
captcha.setFilePath("test.png");
code = DamaUtil.getCaptchaResult(captcha);
System.out.println("打码处理出来的验证码是"+code);
WebElement elementsumbit = driver.findElement(By.id("seccodeInput"));
// 输入关键字
elementsumbit.sendKeys(code);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 提交 input 所在的 form
elementsumbit.submit();
System.out.println("成功"); } }
}

  

以上就代码,关键的代码在Stack Overflow得到的,不得不说谷歌还是很强大的

喜欢呼呼的文章的朋友,可以关注呼呼的个人公众号:

driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo")); // Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot); // Get the location of element on the page
Point point = ele.getLocation(); // Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight(); // Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot); // Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
以上就是关键的截取代码,在国外的链接是http://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-a-specific-element-rather-than-entire-page-usin
感兴趣的小伙伴可以研究一下

selenium WebDriver 截取网站的验证码的更多相关文章

  1. 使用 mitmdump 进行 selenium webDriver绕过网站反爬服务的方法 pdd某宝 可用

    安装:  pip install  mitmproxy 新建一个脚本 脚本代码: from mitmproxy import ctx injected_javascript = ''' // over ...

  2. selenium webdriver 相关网站

    ITeye:http://shijincheng0223.iteye.com/blog/1481446 http://ztreeapi.iteye.com/blog/1750554 http://sm ...

  3. Selenium WebDriver对cookie进行处理绕过登录验证码

    现在几乎所有登录页面都会带一个验证码,做起自动化这块比较麻烦, 所以要绕过网站的验证码. 首先需要手动登录一次你的测试网站,去chrome的F12里获取这个网站的cookie信息,找到对应的保存登录信 ...

  4. (java)selenium webdriver爬虫学习--爬取阿里指数网站的每个分类的top50 相关数据;

    主题:java 爬虫--爬取'阿里指数'网站的每个分类的top50 相关数据: 网站网址为:http://index.1688.com/alizs/top.htm?curType=offer& ...

  5. selenium webdriver (python)的基本用法一

    阅在线 AIP 文档:http://selenium.googlecode.com/git/docs/api/py/index.html目录一.selenium+python 环境搭建........ ...

  6. python利用selenium库识别点触验证码

    利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分 ...

  7. 一行js代码识别Selenium+Webdriver及其应对方案

    有不少朋友在开发爬虫的过程中喜欢使用Selenium + Chromedriver,以为这样就能做到不被网站的反爬虫机制发现. 先不说淘宝这种基于用户行为的反爬虫策略,仅仅是一个普通的小网站,使用一行 ...

  8. Selenium+Webdriver被检测识别出来的应对方案

    在写爬虫,面对很多js 加载的页面,很多人束手无策,更多的人喜欢用Senlenium+ Webdriver,古语有云:道高一尺魔高一丈.已淘宝为首,众多网站都针对 Selenium的js监测机制, 比 ...

  9. 利用selenium库自动执行滑动验证码模拟登陆

    破解流程 #1.输入账号.密码,然后点击登陆 #2.点击按钮,弹出没有缺口的图 #3.针对没有缺口的图片进行截图 #4.点击滑动按钮,弹出有缺口的图 #5.针对有缺口的图片进行截图 #6.对比两张图片 ...

随机推荐

  1. Spring AOP JDK动态代理与CGLib动态代理区别

    静态代理与动态代理 静态代理 代理模式 (1)代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是指静态代理,也就是在代码中显式指定的代理. (2)静态代理由 业务实现类.业务代理类 两部分 ...

  2. 前端面试题集锦(二)之CSS部分

    1.CSS中的选择器都有哪些?权限情况如何? 解答: (1)类选择器 .className  (2) ID选择器 #id  (3) 元素选择器 div 可以多个,以逗号隔开 (4)父子选择器 以空格隔 ...

  3. 偏差和方差以及偏差方差权衡(Bias Variance Trade off)

    当我们在机器学习领域进行模型训练时,出现的误差是如何分类的? 我们首先来看一下,什么叫偏差(Bias),什么叫方差(Variance): 这是一张常见的靶心图 可以看左下角的这一张图,如果我们的目标是 ...

  4. [程序人生]那些IT界“活久见”的奇葩现象

    常言道,人活久了什么稀奇古怪的事都会见到.本文盘点几件刚毕业工作时想当然,工作若干年后啪啪打脸的“奇葩”事. (1)去年推荐一朋友来我们公司面试时,朋友说起当年她去某游戏公司时,那公司HR说这家公司是 ...

  5. 常见rpm包和yum包命令

    1.rpm包 在 安装.升级.卸载服务程序时要考虑到其他程序.库的依赖关系,在进行校验.安装. 卸载.查询.升级等管理软件操作时难度都非常大. RPM 机制则为解决这些问题而设计的.RPM 有点像 W ...

  6. seq2seq通俗理解----编码器和解码器(TensorFlow实现)

    1. 什么是seq2seq 在⾃然语⾔处理的很多应⽤中,输⼊和输出都可以是不定⻓序列.以机器翻译为例,输⼊可以是⼀段不定⻓的英语⽂本序列,输出可以是⼀段不定⻓的法语⽂本序列,例如: 英语输⼊:&quo ...

  7. HDU 4417

    题意略. 思路: 仔细思考这个题目会发现,它其实是要你查询两次,第一是要规定l,r的范围,第二是要在范围内查询小于等于H的个数.所以有的人说要用主席树. 现在,如果我们能省去范围内对h的查询呢?也就是 ...

  8. 搭建Spark高可用集群

      Spark简介 官网地址:http://spark.apache.org/ Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于AI人工智能 sp ...

  9. 2019nc#9

    题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A The power of Fibonacci 点击查看 进入讨论 69/227 未通过 B Quadratic equation 点击查看 ...

  10. 【HDU5409】CRB and Graph 边双联通 子树最值

    HDU # 题意 有一个简单图,n个点,m条边.对于每条割边,求出删去这条边后,在两个联通块中各取一个u,v.使得u<v,并且u尽量大而v尽量小. # 思路 求出边双联通是肯定的. 答案的限制条 ...