Selenium+Tesseract-OCR智能识别验证码爬取网页数据的实例
作者:大数据智工厂 时间:2023-04-11 19:43:01
目录
1.项目需求描述
2.整体思路
3.功能实现
1.项目需求描述
通过订单号获取某系统内订单的详细数据,不需要账号密码的登录验证,但有图片验证码的动态识别,将获取到的数据存到数据库。
2.整体思路
1.通过Selenium技术,无窗口模式打开浏览器
2.在输入框中动态输入订单号
3.将图片验证码截图保存到本地
4.通过Tesseract-OCR技术去本地识别验证码转化为文字
5.将获取的验证码输入输入框
6.点击查询获取列表数据
3.功能实现
1.下载并安装Google浏览器,安装Google驱动chromedriver.exe,获取安装路径,配置在项目中
2.使用Selenium进行浏览器操作
System.setProperty(浏览器驱动, 浏览器驱动安装位置);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless"); // 无窗口模式
options.addArguments("--disable-infobars"); // 禁言消息条
options.addArguments("--disable-extensions"); // 禁用插件
options.addArguments("--disable-gpu"); // 禁用GPU
options.addArguments("--no-sandbox"); // 禁用沙盒模式
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--hide-scrollbars"); // 隐藏滚动条
WebDriver driver = new ChromeDriver(options);
driver.get(爬取网站URL);
driver.manage().window().setSize(new Dimension(450, 260)); // 设置游览器打开后调整大小
try {
// 保存IMG图片到本地
saveImgToLocal(driver);
Thread.sleep(2000);
// OCR智能识别验证码
String codeByOCR = getCodeByOCR();
if (codeByOCR != null) {
try {
WebElement input1 = driver.findElement(By.id(TEXTBOX1));
input1.sendKeys(code);
WebElement input2 = driver.findElement(By.id(TEXTBOX2));
input2.sendKeys(codeByOCR);
// 获取table数据
WebElement addButton = driver.findElement(By.id(SELECT_BUTTON));
addButton.click();
List<WebElement> tRCollection = driver.findElement(By.id(TABLE_ID)).findElements(By.tagName("tr"));
for (int t = 1; t < tRCollection.size(); t++) {
List<WebElement> tDCollection = tRCollection.get(t).findElements(By.tagName("td"));
VipLogisticsMinHangDetailVo minHangDetailVo = new VipLogisticsMinHangDetailVo();
minHangDetailVo.setLogistics_number(code);
for (int i = 0; i < tDCollection.size(); i++) {
String text = tDCollection.get(i).getText();
switch (i) {
case 0:
minHangDetailVo.setTime(text);
case 1:
minHangDetailVo.setOutlet(text);
case 2:
minHangDetailVo.setOrganization(text);
case 3:
minHangDetailVo.setEvent(text);
case 4:
minHangDetailVo.setDetail(text);
}
}
list.add(minHangDetailVo);
}
log.info("验证码识别成功!");
} catch (Exception e) {
if (e.toString().contains("错误提示:验证码错误或已过期!")) {
log.error("验证码识别错误!" + e.toString());
} else if (e.toString().contains("错误提示:请输入验证码!")) {
log.error("未输入验证码!:" + e.toString());
} else {
log.error("其他异常:" + e.toString());
}
}
}
driver.quit();
} catch (Exception e) {
e.printStackTrace();
}
3.将图片验证码截图保存到本地(截屏法)
private void saveImgToLocal(WebDriver driver) {
WebElement element = driver.findElement(By.id(img元素ID));
//创建全屏截图
WrapsDriver wrapsDriver = (WrapsDriver) element;
File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
try {
BufferedImage image = ImageIO.read(screen);
//创建一个矩形使用上面的高度,和宽度
Point p = element.getLocation();
//元素坐标
BufferedImage img = image.getSubimage(p.getX(), p.getY(), element.getSize().getWidth(), element.getSize().getHeight());
ImageIO.write(img, "png", screen);
FileUtils.copyFile(screen, new File(保存本地地址 + "imgname.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
4.将图片验证码保存到本地(鼠标法)
private static void saveImgToLocal1(WebDriver driver) {
Actions action = new Actions(driver);
action.contextClick(driver.findElement(By.id(img元素ID))).build().perform();
try {
Robot robot = new Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
//释放向下键,不然在此之前的条目将起作用
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(1000);
//运行保存
Runtime.getRuntime().exec(SAVE_IMG_EXE);
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
5.对本地验证码进行OCR识别
private String getCodeByOCR() {
String result = null;
File file = new File(本地图片地址);
if (!file.exists()) {
if (systemFalg != 1) {
file.setWritable(true, false);
}
file.mkdirs();
}
File imageFile = new File(本地图片地址 + "imgname.png");
if (imageFile.exists()) {
ITesseract instance = new Tesseract();
instance.setDatapath(tessdata存放地址);
try {
String doOCR = instance.doOCR(imageFile);
result = replaceBlank(doOCR);
log.info("解析的验证码为:{}", result != null ? result : "为空!");
} catch (Exception e) {
e.printStackTrace();
log.error("解析验证码异常!");
}
} else {
log.error("解析验证码的文件不存在!");
}
return result;
}
综上,该网页的数据就可以获取了。
来源:https://www.cnblogs.com/zhaohadoopone/p/15338813.html
标签:Selenium,Tesseract-OCR,验证码,爬取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android UI设计系列之自定义TextView属性实现带下划线的文本框(4)
2022-08-11 11:03:29
![](https://img.aspxhome.com/file/2023/2/137862_0s.png)
SpringBoot整合之SpringBoot整合MongoDB的详细步骤
2023-11-25 09:55:37
![](https://img.aspxhome.com/file/2023/6/59076_0s.png)
Android VelocityTracker使用案例详解
2023-08-29 13:32:03
C++之异常处理详解
2023-04-10 10:22:00
![](https://img.aspxhome.com/file/2023/3/114923_0s.jpg)
关于C# dynamic装箱问题
2021-09-13 19:22:40
Android无需读写权限通过临时授权读写用户文件详解
2022-11-05 12:50:54
![](https://img.aspxhome.com/file/2023/1/137751_0s.png)
C#操作windows系统进程的方法
2023-06-09 05:11:45
RecyclerView中监听EditText变化的BUG的解决方法
2022-08-07 20:44:25
C#程序提示“正由另一进程使用,因此该进程无法访问该文件”的解决办法
2022-06-27 04:34:11
Android性能优化之plt hook与native线程监控详解
2021-09-10 22:06:01
![](https://img.aspxhome.com/file/2023/9/138939_0s.jpg)
UE4 Unlua 调用异步蓝图节点AIMoveTo函数示例详解
2022-04-12 05:35:41
android开发之Json文件的读写的示例代码
2021-12-02 11:55:18
![](https://img.aspxhome.com/file/2023/2/84712_0s.png)
Android实现支付宝手势密码功能
2021-07-26 01:16:34
![](https://img.aspxhome.com/file/2023/1/137491_0s.jpg)
java的Arrays工具类实战
2023-08-21 15:46:36
![](https://img.aspxhome.com/file/2023/6/58076_0s.png)
Java 基础语法
2021-09-21 19:26:32
SpringBoot项目集成Swagger和swagger-bootstrap-ui及常用注解解读
2023-03-17 06:30:20
![](https://img.aspxhome.com/file/2023/2/70952_0s.png)
基于C#实现的屏幕指定区域截屏代码
2021-12-31 19:42:09
Android使用Realm数据库实现App中的收藏功能(代码详解)
2022-11-01 02:00:45
![](https://img.aspxhome.com/file/2023/2/137582_0s.png)
SpringBoot项目中处理返回json的null值(springboot项目为例)
2023-10-26 04:49:06
C#实现获取一年中是第几个星期的方法
2023-12-04 02:02:46