浏览代码

修改爬虫图片验证效果

syh 4 年之前
父节点
当前提交
d6a43968c6

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-dev.yml

@@ -197,6 +197,7 @@ jeecg:
     webapp: D://webapp
     webapp: D://webapp
     video-upload: D://upFiles//video//
     video-upload: D://upFiles//video//
     image-upload: D://upFiles//image//
     image-upload: D://upFiles//image//
+    kuaishou-agent-image: D://temp.png
     chrome-driver: D://chromedriver.exe
     chrome-driver: D://chromedriver.exe
     csv-upload: D://upFiles//csv//
     csv-upload: D://upFiles//csv//
     report-history: D://report//history//
     report-history: D://report//history//

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-prod.yml

@@ -170,6 +170,7 @@ jeecg:
     webapp: /data/webapp
     webapp: /data/webapp
     video-upload: /data/upload/video/
     video-upload: /data/upload/video/
     image-upload: /data/upload/image/
     image-upload: /data/upload/image/
+    kuaishou-agent-image: /data/upload/image/temp.png
     chrome-driver: /usr/bin/chromedriver
     chrome-driver: /usr/bin/chromedriver
     csv-upload: /data/upload/csv/
     csv-upload: /data/upload/csv/
     bak-database-file: /data/data/bak/
     bak-database-file: /data/data/bak/

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-prod2.yml

@@ -162,6 +162,7 @@ jeecg:
     webapp: /data/webapp
     webapp: /data/webapp
     video-upload: /data/upload/video/
     video-upload: /data/upload/video/
     image-upload: /data/upload/image/
     image-upload: /data/upload/image/
+
     chrome-driver: /usr/bin/chromedriver
     chrome-driver: /usr/bin/chromedriver
     csv-upload: /data/upload/csv/
     csv-upload: /data/upload/csv/
     bak-database-file: /data/data/bak/
     bak-database-file: /data/data/bak/

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-test.yml

@@ -158,6 +158,7 @@ jeecg:
     webapp: /data/webapp
     webapp: /data/webapp
     video-upload: /data/upload/video/
     video-upload: /data/upload/video/
     image-upload: /data/upload/image/
     image-upload: /data/upload/image/
+    kuaishou-agent-image: D://temp.png
     chrome-driver: D://chromedriver.exe
     chrome-driver: D://chromedriver.exe
     csv-upload: /data/upload/csv/
     csv-upload: /data/upload/csv/
     report-history: /data/report/history/
     report-history: /data/report/history/

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-wps.yml

@@ -157,6 +157,7 @@ jeecg:
     webapp: /mnt/webapp
     webapp: /mnt/webapp
     video-upload: /mnt/upload/video/
     video-upload: /mnt/upload/video/
     image-upload: /mnt/upload/image/
     image-upload: /mnt/upload/image/
+    kuaishou-agent-image: D://temp.png
     chrome-driver: D://chromedriver.exe
     chrome-driver: D://chromedriver.exe
     csv-upload: /mnt/upload/csv/
     csv-upload: /mnt/upload/csv/
     bak-database-file: /mnt/data/bak/
     bak-database-file: /mnt/data/bak/

+ 2 - 2
jeecg-boot-module-system/src/main/resources/application.yml

@@ -1,7 +1,7 @@
 spring:
 spring:
   profiles:
   profiles:
-    active: @activatedProperties@
-#    active: wps
+#    active: @activatedProperties@
+    active: wps
 swagger:
 swagger:
   production: false
   production: false
   basic:
   basic:

+ 33 - 16
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/service/impl/KuaishouReportDailyAgentServiceImpl.java

@@ -21,10 +21,11 @@ import org.apache.commons.io.FileUtils;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.impl.cookie.BasicClientCookie;
 import org.apache.http.impl.cookie.BasicClientCookie;
 import org.jeecg.common.util.DateUtils;
 import org.jeecg.common.util.DateUtils;
-import org.openqa.selenium.*;
 import org.openqa.selenium.NoSuchElementException;
 import org.openqa.selenium.NoSuchElementException;
+import org.openqa.selenium.*;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.chrome.ChromeOptions;
 import org.openqa.selenium.chrome.ChromeOptions;
+import org.openqa.selenium.interactions.Actions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -54,19 +55,32 @@ public class KuaishouReportDailyAgentServiceImpl extends ServiceImpl<KuaishouRep
     private KuaishouAgentAccountMapper kuaishouAgentAccountMapper;
     private KuaishouAgentAccountMapper kuaishouAgentAccountMapper;
     @Value("${jeecg.path.chrome-driver}")
     @Value("${jeecg.path.chrome-driver}")
     private String chromeDriver;
     private String chromeDriver;
+    @Value("${jeecg.path.kuaishou-agent-image}")
+    private String kuaishouAgentImage;
 
 
     @Override
     @Override
     public void replaceBatch(List<KuaishouReportDailyAgent> list) {
     public void replaceBatch(List<KuaishouReportDailyAgent> list) {
         kuaishouReportDailyAgentMapper.replaceBatch(list);
         kuaishouReportDailyAgentMapper.replaceBatch(list);
     }
     }
 
 
+    private void tapPoint(WebDriver driver, Integer x, Integer y) {
+        WebElement element = driver.findElement(By.tagName("iframe"));
+        Actions actions = new Actions(driver);
+        actions.moveToElement(element,x,y).click().perform();
+        try {
+            Thread.sleep(500L);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+    }
+
     public boolean loginAgent() {
     public boolean loginAgent() {
         boolean loginStatus = false;
         boolean loginStatus = false;
         System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
         System.getProperties().setProperty("webdriver.chrome.driver", chromeDriver);
         String url = "https://agent.e.kuaishou.com";
         String url = "https://agent.e.kuaishou.com";
         ChromeOptions chromeOptions = new ChromeOptions();
         ChromeOptions chromeOptions = new ChromeOptions();
-//        chromeOptions.addArguments("--headless");
-//        chromeOptions.addArguments("--no-sandbox");
+        chromeOptions.addArguments("--headless");
+        chromeOptions.addArguments("--no-sandbox");
         chromeOptions.addArguments("--disable-dev-shm-usage");
         chromeOptions.addArguments("--disable-dev-shm-usage");
         chromeOptions.addArguments("--incognito");
         chromeOptions.addArguments("--incognito");
         chromeOptions.addArguments("--disable-gpu");
         chromeOptions.addArguments("--disable-gpu");
@@ -95,15 +109,21 @@ public class KuaishouReportDailyAgentServiceImpl extends ServiceImpl<KuaishouRep
                 log.error("【错误】快手登录出现验证码!");
                 log.error("【错误】快手登录出现验证码!");
                 File scrFile = captureElement(webDriver.findElement(By.tagName("iframe")));
                 File scrFile = captureElement(webDriver.findElement(By.tagName("iframe")));
                 //clickCharBg
                 //clickCharBg
-                String savePath = "D:/temp.png";
-                FileUtils.copyFile(scrFile, new File(savePath));
-                webDriver.switchTo().frame(webDriver.findElement(By.tagName("iframe")));
-                String result = ChaojiyingUtils.process(Base64Utils.imageToBase64(savePath),9104);
-                System.out.println(result);
+                FileUtils.copyFile(scrFile, new File(kuaishouAgentImage));
+                String result = ChaojiyingUtils.process(Base64Utils.imageToBase64(kuaishouAgentImage),9104);
                 String pointString = JSONObject.parseObject(result).getString("pic_str");
                 String pointString = JSONObject.parseObject(result).getString("pic_str");
-
-                ((JavascriptExecutor) webDriver).executeScript("arguments[0].click();", webDriver.findElement(By.className("aq_icon_refresh")));
+                String[] split = pointString.split("\\|");
+                if(split.length>0){
+                    for(int i=0;i<split.length;i++){
+                        String pointStr = split[i];
+                        String [] taps = pointStr.split(",");
+                        tapPoint(webDriver,Integer.parseInt(taps[0]),Integer.parseInt(taps[1]));
+                    }
+                }
+                webDriver.switchTo().frame(webDriver.findElement(By.tagName("iframe")));
+                webDriver.findElement(By.xpath("//div[@class='opArea']/span[@id='submit']")).click();
             }
             }
+            Thread.sleep(3000L);
             webDriver.get("https://agent.e.kuaishou.com/#/index");
             webDriver.get("https://agent.e.kuaishou.com/#/index");
             Thread.sleep(3000L);
             Thread.sleep(3000L);
             Set<Cookie> cookies = webDriver.manage().getCookies();
             Set<Cookie> cookies = webDriver.manage().getCookies();
@@ -144,18 +164,15 @@ public class KuaishouReportDailyAgentServiceImpl extends ServiceImpl<KuaishouRep
     }
     }
 
 
     public File captureElement(WebElement element) throws Exception {
     public File captureElement(WebElement element) throws Exception {
-        // TODO Auto-generated method stub
         WrapsDriver wrapsDriver = (WrapsDriver) element;
         WrapsDriver wrapsDriver = (WrapsDriver) element;
         // 截图整个页面
         // 截图整个页面
         File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
         File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
-//        ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.)
-        String tempPath = "D:/temp.png";
-        FileUtils.copyFile(screen, new File(tempPath));
-        BufferedImage img = ImageIO.read(new File(tempPath));
+        FileUtils.copyFile(screen, new File(kuaishouAgentImage));
+        BufferedImage img = ImageIO.read(new File(kuaishouAgentImage));
         // 获得元素的高度和宽度
         // 获得元素的高度和宽度
         int width = element.getSize().getWidth();
         int width = element.getSize().getWidth();
         int height = element.getSize().getHeight();
         int height = element.getSize().getHeight();
-//        // 得到元素的坐标
+        // 得到元素的坐标
         Point p = element.getLocation();
         Point p = element.getLocation();
         BufferedImage dest = img.getSubimage(p.x, p.y, width, height);
         BufferedImage dest = img.getSubimage(p.x, p.y, width, height);
         // 存为png格式
         // 存为png格式