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

一 刚开始的思路就是打开这个验证码的链接,然后通过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. Android lifecycle 使用详解

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/gdutxiaoxu/article/det ...

  2. c语言的图形库

    图形库链接http://www.easyx.cn/ 使用图形库头文件easyx.h或graphics.h 同样在里面下载图形库帮助文档进行查询 vs vc都可使用图形库 图形库窗口: initgrap ...

  3. 《C# 7.0核心技术指南》到货

    前几天有大佬推荐本书,并且折扣相当的划算,随入手一本.

  4. Python-demo(photo)

    import osimport urllib import requests#import wximport time from fake_useragent import UserAgentfrom ...

  5. cf 1102 B

    题意:求字符串中任意相邻两位是否可以可以由前一个加上任意个x或y屏蔽十位与后一位相等,如果可以需要添加的最少数字是多少,x值为0-9,y值也为0-9,求出任意x,y对应情形下字符串需要添加的最少数字, ...

  6. 【入门】广电行业DNS、DHCP解决方案详解(一)——历史及现状篇

    广电发展历史 单项网络 双向网络 智能网络 广电网络现状 广电网络典型特征 接入技术混杂 承载业务多样化 业务终端错综复杂 其他 网络现状模型 总结 广电发展历史 广电就是广播电视,广电发展可以分为三 ...

  7. 在.net core web 项目中操作MySql数据库(非ORM框架,原生sql语句方式)

    本案例通过MySql.Data和Dapper包执行原生sql,实现对数据库的操作. 操作步骤: 第1步:在MySql数据库中新建表User(使用Navicat For MySql工具) 建表语句: c ...

  8. c++ uconcontext.h实现协程

    目录 c++ uconcontext.h实现协程 什么是协程? ucontext.h库 库的使用示例 代码地址 c++ uconcontext.h实现协程 什么是协程? 协程是一种程序组件,是由子例程 ...

  9. hdu 2615 Division(暴力)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2615 题解:挺简单的暴力枚举,小小的分治主要是看没人写题解就稍微写一下 #include <io ...

  10. codeforces 459 E. Pashmak and Graph(dp)

    题目链接:http://codeforces.com/contest/459/problem/E 题意:给出m条边n个点每条边都有权值问如果两边能够相连的条件是边权值是严格递增的话,最长能接几条边. ...