java+selenium爬取图片签名的方法
程序员文章站
2023-11-24 20:33:22
本文实例为大家分享了java+selenium爬取图片签名的具体实现方法,供大家参考,具体内容如下
学习记录:
1.注意
对应的版本非常重要,使用selenium得下...
本文实例为大家分享了java+selenium爬取图片签名的具体实现方法,供大家参考,具体内容如下
学习记录:
1.注意
对应的版本非常重要,使用selenium得下载与游览器版本相对应的插件,有火狐和谷歌我用的谷歌,贴下
查看谷歌版本:
2.插件存放路径
3.获取签名图片存放路径
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.结果图
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。