温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在java中使用selenium爬取图片签名

发布时间:2021-05-21 16:24:36 来源:亿速云 阅读:209 作者:Leah 栏目:编程语言

这期内容当中小编将会给大家带来有关如何在java中使用selenium爬取图片签名,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1.注意

对应的版本非常重要,使用selenium得下载与游览器版本相对应的插件,有火狐和谷歌我用的谷歌,贴下谷歌driver的插件

查看谷歌版本:

如何在java中使用selenium爬取图片签名

2.插件存放路径

如何在java中使用selenium爬取图片签名

3.获取签名图片存放路径

如何在java中使用selenium爬取图片签名

4.Controller代码如下

 @ResponseBody  @RequestMapping(value = "signatureGenerationv")  public String signatureGeneration(String userName, HttpServletRequest request) throws Exception{  System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");  //初始化一个谷歌浏览器实例,实例名称叫driver  WebDriver driver = new ChromeDriver();  // get()打开一个站点  driver.get("https://www.yishuzi.cn/qianming/");  //签名  driver.findElement(By.xpath("//*[@id=\"text\"]")).clear();  driver.findElement(By.xpath("//*[@id=\"text\"]")).sendKeys(userName);  //签名配色  driver.findElement(By.xpath("//*[@id=\"index\"]/div[1]/div/div[4]/div[2]/div/div[7]/div/div/p[2]/a[2]")).click();  //最大化窗口  driver.manage().window().maximize();  Thread.sleep(2000);  WebElement webElement = driver.findElement(By.xpath("//*[@id=\"SaveImg\"]"));  //生成签名图片,裁剪图片  byte[] imageData = BrowserUtil.captureElement(webElement);  //设置隐性等待时间  driver.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);  //getTitle()获取当前页面title的值  System.out.println("当前打开页面的标题是: "+ driver.getTitle());  //关闭并退出浏览器  driver.quit();  //把图片转换成能够在页面直接展示的BASE64Encoder格式  BASE64Encoder base64Encoder = new BASE64Encoder();  String img = base64Encoder.encode(imageData);  return img;  }

5.BrowserUtil工具类代码

public static byte[] captureElement(WebElement element) throws Exception {  WrapsDriver wrapsDriver = (WrapsDriver) element;  // 截图整个页面  byte[] screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).getScreenshotAs(OutputType.BYTES);  ByteArrayOutputStream out = new ByteArrayOutputStream();  try {   BufferedImage img = ImageIO.read(new ByteArrayInputStream(screen));   ImageIO.write(img, "png", new File("img/imgs/img"+System.currentTimeMillis()+".png"));   // 获得元素的高度和宽度   int width = element.getSize().getWidth();   int height = element.getSize().getHeight();   // 创建一个矩形使用上面的高度,和宽度   java.awt.Rectangle rect = new java.awt.Rectangle(width, height);   // 得到元素的坐标   Point p = element.getLocation();   float rate = img.getWidth()/1280;   BufferedImage dest = img.getSubimage(    (int)(p.getX()*rate),    (int)(p.getY()*rate),    (int)(rect.width*rate),(int)(rect.height*rate));   ImageIO.write(dest, "png", new File("img/"+System.currentTimeMillis()+".png"));   boolean flag = ImageIO.write(dest, "png", out);   byte[] imageData = out.toByteArray();   return imageData;  } finally {   out.close();  }  }

6.index.html代码

<!DOCTYPE html> <html lang="en"> <head>  <meta charset="UTF-8">  <title>签名生成</title>  <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script> </head> <body> <div>  请输入名字:<input type="text" id="userName" name="userName"><input type="button" id="btn" value="生成">  <!--<img id="img" src="">-->  <div id="img"></div> </div> <script>  $("#btn").click(function () {   var userName = $("#userName").val();   $.ajax({   url : "signatureGenerationv",   type : "post",   // dataType :"string",   data :{'userName':userName},   success : function(data) {    data = data.replace(/\\n/g,'\n')//去换行    var str = data.substring(0,data.length)    var base = "data:image/png;base64,"+str;    var img = new Image();//创建img容器    img.src = base;    document.body.appendChild(img);   },   error : function(data) {    alert("失败"+data.responseText)   }   })  }) </script> </body> </html>

7.结果图

如何在java中使用selenium爬取图片签名

8.pom包

<!--selenium依赖文件-->  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->  <dependency>   <groupId>org.seleniumhq.selenium</groupId>   <artifactId>selenium-java</artifactId>   <version>3.8.1</version>  </dependency>  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->  <dependency>   <groupId>org.seleniumhq.selenium</groupId>   <artifactId>selenium-server</artifactId>   <version>3.8.1</version>  </dependency>  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->  <dependency>   <groupId>org.seleniumhq.selenium</groupId>   <artifactId>selenium-chrome-driver</artifactId>   <version>3.8.1</version>  </dependency>  <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->  <dependency>   <groupId>org.seleniumhq.selenium</groupId>   <artifactId>selenium-chrome-driver</artifactId>   <version>3.8.1</version>  </dependency>  <dependency>   <groupId>org.seleniumhq.selenium</groupId>   <artifactId>selenium-htmlunit-driver</artifactId>   <version>2.52.0</version>  </dependency>  <dependency>   <groupId>com.google.code.gson</groupId>   <artifactId>gson</artifactId>   <version>2.8.5</version>  </dependency>

Java可以用来干什么

Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页开发;5. 企业级应用开发;6. Java大数据开发;7.游戏开发等。

上述就是小编为大家分享的如何在java中使用selenium爬取图片签名了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI