前言
谷歌瀏覽器自動化--安裝地址:https://www.cnblogs.com/kawhileonardfans/articles/10965856.html
我上次的需求是做一個爬蟲,爬取一些網站的敏感信息,然后要把這個網頁敏感信息的證據保存下來,我們這里會保存兩種,第一種就是網頁內容(HTML),第二種就是我們現在說的截圖,把這個網頁保存為一張圖片。
這篇文章的方式是通過selenium操作谷歌瀏覽器進行截圖,當然也可以操作火狐瀏覽器截圖(個人感覺比谷歌瀏覽器效果好,沒谷歌這么多問題,比如說谷歌截圖截不全)等;除了通過selenium操作瀏覽器外,我這里還有一種方式,是通過PHANTOMJS對網頁截屏,效果不錯,請看下面鏈接:
使用PHANTOMJS對網頁截屏地址:https://www.cnblogs.com/kawhileonardfans/articles/10965906.html
案例一:保存網頁可見區域為圖片
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://www.baidu.com/"); //找到百度上面的輸入框、放入輸入內容‘鹿晗人妖’ driver.findElement(By.id("kw")).sendKeys("鹿晗人妖"); //點擊百度旁邊的搜索按鈕 driver.findElement(By.id("su")).click(); //暫停兩秒,讓他加載搜索出來的數據 Thread.sleep(2000); //對整個網頁截圖 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //把截圖保存到桌面 FileUtils.copyFile(srcFile, new File("C:\\Users\\Administrator\\Desktop\\1233.png")); driver.quit(); }
案例二:保存網頁可見區域中的某一塊為圖片
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://tool.oschina.net/highlight"); Thread.sleep(2000); //找到class為wrapper的節點 WebElement webElement = driver.findElement(By.className("wrapper")); Point point = webElement.getLocation(); int eleWidth = webElement.getSize().getWidth(); int eleHeight = webElement.getSize().getHeight(); //對整個網頁截圖 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //在上面的網頁截圖中,把根據class找到的節點截取出來、並覆蓋上面的網頁截圖 BufferedImage fullImg = ImageIO.read(srcFile); BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); ImageIO.write(eleScreenshot, "png", srcFile); //把根據class找到的節點截圖保存到桌面 FileUtils.copyFile(srcFile, new File("C:\\Users\\Administrator\\Desktop\\1233.png")); driver.quit(); }

案例三:保存網頁可見區域為圖片、並且標記網頁中的關鍵字
public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://news.baidu.com"); //獲取百度新聞中html String htmlContent = driver.getPageSource(); //解析html字符串(引入了jsoup-1.8.1.jar) Document document = Jsoup.parse(htmlContent); //刪除html下面標簽中的onclick屬性、href屬性(我這里只是截圖、點擊事件對我沒用) for (Element element : document.getAllElements()) { element.removeAttr("onclick").removeAttr("href"); } //刪除html下面所有的script標簽(我這里只是截圖、不需要動態頁面) for (Element element : document.getElementsByTag("script")) { element.remove(); } //替換html中的雙引號為單引號、刪除換行 String reHtmlContent = document.body().html().replace("\"", "'").replaceAll("\r|\n", "");; //標記'網頁'為敏感字、用紅色框給他框住 reHtmlContent = reHtmlContent.replace("網頁", "<span style='border:2px solid red;'>網頁</span>"); reHtmlContent = "\"" + reHtmlContent + "\""; //通過js把轉換完的html替換到頁面的body上面 JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.body.innerHTML=" + reHtmlContent); //對整個網頁截圖 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //把截圖保存到桌面 FileUtils.copyFile(srcFile, new File("C:\\Users\\Administrator\\Desktop\\1233.png")); driver.quit(); }
案例四:保存網頁為圖片(上面的案例只會保存可見區域)
import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import com.xjxcc.util.ImageUtils; public class Test1 { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://zhidao.baidu.com"); /* 通過js獲取瀏覽器的各種高度 */ JavascriptExecutor js = (JavascriptExecutor) driver; String heightStrs = (String) js.executeScript("return document.body.scrollHeight.toString()+','+document.body.scrollTop.toString() + ',' + window.screen.height.toString()"); String[] heights = heightStrs.split(","); int htmlHeight = Integer.parseInt(heights[0]);//整個頁面的高度 int scrollTop = Integer.parseInt(heights[1]);//滾動條現在所處的高度 int screenHeight = Integer.parseInt(heights[2]);//電腦屏幕的高度 screenHeight = screenHeight - 140; //開始滾動截圖 int count = 0; while(scrollTop < htmlHeight){ scrollTop += screenHeight; System.out.println("document.body.scrollTop = " + screenHeight * count); ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, "+ (screenHeight * count) +")"); //對整個網頁截圖 File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //把截圖保存到桌面 FileUtils.copyFile(srcFile, new File("C:\\Users\\Administrator\\Desktop\\allImg\\"+ (++count) +".png")); } //拼接圖片 File imgsFile = new File("C:\\Users\\Administrator\\Desktop\\allImg"); if(!imgsFile.isDirectory()){ throw new RuntimeException("地址不是一個正確的目錄..."); } File[] imgsFiles = imgsFile.listFiles(); ImageUtils.mergeImg(imgsFiles, ImageUtils.IMG_TYPE_PNG, ImageUtils.MERGE_IMG_TYPE_Y, "C:\\Users\\Administrator\\Desktop\\111.png"); driver.quit(); } }