Use selenium + Java para rastrear datos HowNet para resolver el problema del código de verificación

La solución para rastrear los datos de HowNet encontró el código de verificación de imagen

Descripción detallada e ideas:
1: en el proceso de rastrear datos HowNet utilizando selenio, el método de suspensión de hilos se usó para engañar al principio, pero luego se descubrió que este método no podía resolver el problema de más de cien páginas. Más tarde, cambié a un método para resolver el código de verificación de imagen. Solo pensé en usar OCR para identificar, pero el efecto no es bueno. El último pensamiento es llamar a una interfaz de terceros para identificar el código de verificación, la idea es la siguiente:
1) Primero tome una captura de pantalla, intercepte el código de verificación por captura de pantalla y guárdelo en una ubicación determinada.
2) Llame a una interfaz de terceros para identificar el código de verificación en la captura de pantalla y completarlo en el cuadro de entrada del código de verificación. Debido a que no se reconoce al 100% cada vez, repita el reconocimiento muchas veces para conocer la posición correcta.
Nota: La interfaz de terceros utiliza la interfaz de reconocimiento de texto de Baidu. Para obtener más información, haga clic en:
http://ai.baidu.com/tech/ocr

Parte del código central es el siguiente:

  // if (i % 15 == 0) {
                // Thread.sleep(20000);
                WebElement bodyEle = driver.findElement(By.tagName("body"));
                List<WebElement> list = bodyEle.findElements(By.tagName("input"));
                /////////////////////////////////////////////////////
                //  注意:截图和识别是一个连续的过程,如果验证码识别出错,那么久无法进行到下一步,那么就该继续截图识别
                //  java 截图
                //  获取验证码的位置在屏幕中 //*[@id="CheckCodeImg"]
                // WebElement checkImage = driver.findElement(By.id("CheckCodeImg"));
                // int x = checkImage.getLocation().getX();
                // int y = checkImage.getLocation().getY();
                // System.out.println(x);
                //  System.out.println(y);
                /*WebElement checkImage2 = driver.findElement(By.id("CheckCodeImg"));
                String message = checkImage2.getAttribute("src");
                System.out.println("X:" + checkImage2.getLocation().getX());
                System.out.println("Y:" + checkImage2.getLocation().getY());
*/
                //WebElement checkImage2 = driver.findElement(By.id("CheckCodeImg"));
                //  /html/body/p[1]/label
                WebElement text1 = driver.findElement(By.xpath("/html/body/p[1]/label"));
                String text1Str = text1.getText();
                System.out.println(text1.getText());
                // while (text1Str.equals("请输入验证码")) {   //  验证码一直存在,就一直截图验证
                do {
                    pageCount++;
                    WebElement checkImage2 = driver.findElement(By.id("CheckCodeImg"));
                    // String message = checkImage2.getAttribute("src");
                    int X = checkImage2.getLocation().getX();
                    int Y = checkImage2.getLocation().getY();
                    System.out.println(X + "    " + Y);
                    // System.out.println("X:" + checkImage2.getLocation().getX());
                    //  System.out.println("Y:" + checkImage2.getLocation().getY());
                    File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
                    byte[] bytes = File2byte(scrFile);
                    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                    BufferedImage image = ImageIO.read(bais);

                    //  修改图片存放的位置
                    File pathCheck = new File(ResourceUtils.getURL("classpath:").getPath());
                    if (!pathCheck.exists()) pathCheck = new File("");
                    // System.out.println("path:"+path1.getAbsolutePath());
                    File uploadCheck = new File(pathCheck.getAbsolutePath(), "src/main/webapp/checkCodeImage");
                    if (!uploadCheck.exists()) uploadCheck.mkdirs();
                    String pathKey = uploadCheck.getAbsolutePath() + "\\screenfile.png";   //  这里最终是  detailUrl.txt
                    // 路径文件
                  //  File filekey = new File(pathKey);
                    File screenFile = new File(pathKey);

                    // 如果文件夹路径不存在,则创建
                    if (!screenFile.getParentFile().exists()) {
                        screenFile.getParentFile().mkdirs();
                    }
                    //  图片的像素为   63   22   坐标为:469   39
                    //  截取这儿是一个问题  因为如果第一次不对  那么 图片的位置就会变化
                    //  首先获取图片的位置  两次图片的位置 如:   412, 40, 63, 22
                    //  第一次: X:469  Y:39
                    //  第二次: X:469   Y:76
                    BufferedImage subimage = image.getSubimage((X - 63), Y, 63, 22);
                    ImageIO.write(subimage, "png", screenFile);
                    // ImageIO.write(image, "png", screenFile);
                    // Thread.sleep(10000);
                    //   验证码的识别
                    // 初始化一个AipOcr
                    AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
                    // 可选:设置网络连接参数
                    client.setConnectionTimeoutInMillis(2000);
                    client.setSocketTimeoutInMillis(60000);
                    // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
                    // 也可以直接通过jvm启动参数设置此环境变量
                    System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
                    // 调用接口
                    //String path = "E:\\Images\\screenfile.png";
                    org.json.JSONObject res = client.basicGeneral(pathKey, new HashMap<String, String>());
                    net.sf.json.JSONObject myJson = net.sf.json.JSONObject.fromObject(res.toString());
                    Map m = myJson;
                    Object object = m.get("words_result");
                    JSONArray json = JSONArray.fromObject(object);
                    List<Map<String, Object>> mapListJson = json;
                    Map<String, Object> checkMap = mapListJson.get(0);
                    String key = (String) checkMap.get("words");
                    System.out.println(key);
                    //  输入框
                    WebElement inputEle = driver.findElement(By.id("CheckCode"));//list.get(0);
                    //inputEle.sendKeys("123");
                    inputEle.sendKeys(key);
                    Thread.sleep(5000);
                    //  提交按钮
                    WebElement submitButn = driver.findElement(By.xpath("/html/body/p[1]/input[2]"));//list.get(1);
                    submitButn.click();
                    //  如果页数超过60那么就休息两分钟
                    if (i % 60 == 0 || i % 70 == 0 || i % 90 == 0){
                        Thread.sleep(12000);
                    }
                } while (text1Str.equals("请输入验证码"));

                //   }

                System.out.println("退出循环了,验证通过了");

                /*File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
                byte[] bytes = File2byte(scrFile);
                ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
                BufferedImage image = ImageIO.read(bais);
                File screenFile = new File("E:\\Images\\screenfile.png");
                // 如果文件夹路径不存在,则创建
                if (!screenFile.getParentFile().exists()) {
                    screenFile.getParentFile().mkdirs();
                }
                //  图片的像素为   63   22   坐标为:469   39
                BufferedImage subimage = image.getSubimage(412, 40, 63, 22);
                ImageIO.write(subimage, "png", screenFile);
                // Thread.sleep(10000);
                //   验证码的识别
                // 初始化一个AipOcr
                AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
                // 可选:设置网络连接参数
                client.setConnectionTimeoutInMillis(2000);
                client.setSocketTimeoutInMillis(60000);
                // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
                // 也可以直接通过jvm启动参数设置此环境变量
                System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
                // 调用接口
                String path = "E:\\Images\\screenfile.png";
                org.json.JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
                net.sf.json.JSONObject myJson = net.sf.json.JSONObject.fromObject(res.toString());
                Map m = myJson;
                Object object = m.get("words_result");
                JSONArray json = JSONArray.fromObject(object);
                List<Map<String, Object>> mapListJson = (List) json;
                Map<String, Object> checkMap = mapListJson.get(0);
                String key = (String) checkMap.get("words");
                System.out.println(key);
                //  输入框
                WebElement inputEle = list.get(0);
                inputEle.sendKeys(key);
                Thread.sleep(2000);
                //  提交按钮
                WebElement submitButn = list.get(1);
                submitButn.click();*/
                //Thread.sleep(5000);

El código solo involucra la parte de captura de pantalla e identificación, y la parte con muchos comentarios no se ha eliminado. Quiero grabarlo en detalle para pensarlo más tarde. En cuanto a las funciones, como la búsqueda simulada de usuarios, si necesita internautas, comente o chatee a continuación para ayudarlo a resolverlo.

13 artículos originales publicados · Me gusta1 · Visita 2006

Supongo que te gusta

Origin blog.csdn.net/qq_31152023/article/details/100066460
Recomendado
Clasificación