摘要:引言在監(jiān)控系統(tǒng)網(wǎng)頁爬蟲審核流程等領(lǐng)域,經(jīng)常需要保存系統(tǒng)在某一時刻的狀態(tài)網(wǎng)頁快照。之后使用另一個命令行訪問本地的端口能夠看到調(diào)試信息應(yīng)該就是安裝成功了下載提供了操作的,是控制的橋梁。
引言
在監(jiān)控系統(tǒng)、網(wǎng)頁爬蟲、審核流程等領(lǐng)域,經(jīng)常需要保存系統(tǒng)在某一時刻的狀態(tài)-網(wǎng)頁快照。實(shí)現(xiàn)網(wǎng)頁快照主要有phantomjs、selenium,但是網(wǎng)上資料都是零碎的,本文主要采用Selenium,詳細(xì)闡述從編碼到部署的整個流程.
一、添加Selenium依賴二、實(shí)現(xiàn)網(wǎng)頁快照org.seleniumhq.selenium selenium-java 3.11.0
(1) 創(chuàng)建WebDriver
a.手機(jī)設(shè)備
public WebDriver createWebDriver(String device) { MapmobileEmulation = new HashMap (); //設(shè)置設(shè)備,例如:Google Nexus 7/Apple iPhone 6 //mobileEmulation.put("deviceName", "Google Nexus 7"); if(StringUtils.isBlank(device)) device = "iPhone 6"; mobileEmulation.put("deviceName", device); //這里是要使用的模擬器名稱,就是瀏覽器中模擬器中的頂部型號 Map chromeOptions = new HashMap (); chromeOptions.put("mobileEmulation", mobileEmulation); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions); WebDriver driver = new ChromeDriver(capabilities); Dimension dimension = new Dimension(420, 900); driver.manage().window().setSize(dimension); return driver; }
b.PC設(shè)備
public WebDriver createWebDriver(String device) { //暫時采用特定分辨率 ChromeOptions options = new ChromeOptions(); options.addArguments("window-size=1200,800"); WebDriver driver = new ChromeDriver(options); driver.manage().window(); return driver; }
(2) 繪制圖像
public String execute(String uri, String device) { PerfCounter.count("miui_ad_schedule_capture.execute.total", 1L); long start = System.currentTimeMillis(); System.setProperty("webdriver.chrome.driver", "/home/rd/chromedriver"); //windows: System.setProperty("webdriver.chrome.driver", "src/main/resources/driver/chromedriver.exe"); WebDriver driver = createWebDriver(device); String cdnUrl = ""; try { driver.get(uri); cdnUrl = savePage(driver, url, "capture_" + System.currentTimeMillis() + ".png"); } catch (Exception e) { logger.error("capture fail: [{},{}],{} !", uri ,device,e); } finally { driver.close(); driver.quit(); } PerfCounter.count("miui_ad_schedule_capture.execute.success", 1L, System.currentTimeMillis() - start); return cdnUrl; } private String savePage(WebDriver driver, String filePath, String fileName) throws IOException, InterruptedException, NoSuchAlgorithmException { JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; TakesScreenshot takesScreenshot = (TakesScreenshot) driver; jsExecutor.executeScript("window.scrollTo(0,0)"); BufferedImage imageOriginal = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);// 創(chuàng)建全屏截圖 int lastScroll = -1; int currentScroll = 0; Thread.sleep(1000); while (lastScroll != currentScroll) { byte[] bytesScroll = takesScreenshot.getScreenshotAs(OutputType.BYTES); BufferedImage imageScroll = ImageIO.read(new ByteArrayInputStream(bytesScroll)); int screenHeight = imageScroll.getHeight(); int screenWidth = imageScroll.getWidth(); System.out.println("scree:" + screenHeight + ":" + screenWidth); BufferedImage combined = new BufferedImage(screenWidth, currentScroll + screenHeight, BufferedImage.TYPE_INT_RGB); Graphics g = combined.getGraphics(); g.drawImage(imageOriginal, 0, 0, null); g.drawImage(imageScroll, 0, currentScroll, null); imageOriginal = combined; logger.info("lastScroll:" + lastScroll + " currentScroll:" + currentScroll + " screenHeight:" + screenHeight); int scrollTo = currentScroll + screenHeight; lastScroll = currentScroll; jsExecutor.executeScript("window.scrollTo(0," + scrollTo + ")"); currentScroll = Double.valueOf(jsExecutor.executeScript("return document.documentElement.scrollTop").toString()).intValue(); System.out.println(screenHeight + ":" + scrollTo); if (lastScroll > 5000) { break; } Thread.sleep(1000); } File path = new File(filePath); if (!path.exists() || !path.isDirectory()) { path.mkdirs(); } File file = new File(path.getAbsolutePath() + File.separatorChar + fileName); ImageIO.write(imageOriginal, "png", file); String uploadPath = FileStorageUtils.upload(file.getAbsolutePath(), CHANNEL_ID); System.out.println("uploadPath:" + uploadPath); if(file.exists()){ file.delete(); } return uploadPath; }三、部署(ubantu16.04)
(1) 安裝chrome
sudo apt-get install libxss1 libappindicator1 libindicator7 #install dependency wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome*.deb sudo apt-get install -f
安裝完成后測試:
google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu
這里是使用headless模式進(jìn)行遠(yuǎn)程調(diào)試,ubuntu上大多沒有g(shù)pu,所以--disable-gpu以免報錯。之后使用另一個命令行訪問本地的9222端口:
curl http://localhost:9222
能夠看到調(diào)試信息應(yīng)該就是安裝成功了
(2) 下載chromedriver
chromedriver提供了操作chrome的api,是selenium控制chrome的橋梁。
可以到:https://sites.google.com/a/ch...。
下載并解壓:
wget https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip unzip chromedriver_linux64.zip
Chrome與WebDriver的版本對應(yīng)關(guān)系:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
(3) 安裝Xfvb
如果不安裝Xfvb,java -jar xxx.jar 將報一下錯誤:
Starting ChromeDriver 2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57) on port 14103 Only local connections are allowed. Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.9.15-x86_64-linode81 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 60.13 seconds Build info: version: "unknown", revision: "unknown", time: "unknown" System info: host: "localhost", ip: "127.0.0.1", os.name: "Linux", os.arch: "amd64", os.version: "4.9.15-x86_64-linode81", java.version: "1.8.0_131" Driver info: driver.version: ChromeDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ...
安裝方式:
a.安裝Xvfb
sudo apt install Xvfb
b.啟動Xvfb服務(wù)
Xvfb -ac :7 -screen 0 1280x1024x8 &
c.連接服務(wù)7
export DISPLAY=:7
關(guān)于Xvfb的具體應(yīng)用請參考:https://www.x.org/archive/X11R7.6/doc/man/man1/Xvfb.1.xhtml
參考鏈接:
https://blog.csdn.net/codebat...
https://jiayi.space/post/zai-...
博客編寫不易,如果覺得寫得可以,請幫忙點(diǎn)個贊
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/69125.html
摘要:表示少女與緊耦合在它的構(gòu)造函數(shù)中自行創(chuàng)建了。面向切面編程往往被定義為促使軟件系統(tǒng)實(shí)現(xiàn)關(guān)注點(diǎn)的分離一項(xiàng)技術(shù)系統(tǒng)由許多不同的組件組成,每個組件各負(fù)責(zé)一特定的功能。我們可以把切面想象為覆蓋在很多組件之上的一個外殼。 第1章 Spring之旅 說明 1、本文抄寫了《Spring 實(shí)戰(zhàn)》重點(diǎn)內(nèi)容,參考了GitHub上的代碼 2、每個人的學(xué)習(xí)方式不一樣,但目的是一樣的,活學(xué)活用。最近一直在聽《我...
摘要:預(yù)知后事如何,且聽下回分解六心得體會經(jīng)歷了這次彎彎彎彎彎的極度不暢的建站之路,我才明白高屋建瓴的重要意義,也明白了循序漸進(jìn)先打好基礎(chǔ)才是王道呀。? 一、背景 為了達(dá)到創(chuàng)1的結(jié)課要求,并且實(shí)現(xiàn)以前就憧憬過的網(wǎng)站想法,我在什么都沒有準(zhǔn)備的情況下開始了建設(shè)網(wǎng)站的腳步。腦袋一熱就行動,也許我就這樣子的莽撞,造成我后來撞得滿頭包。 二、預(yù)備知識 開始的時候我只有學(xué)了一個學(xué)期的c語言,java,...
摘要:分布式架構(gòu)實(shí)踐負(fù)載均衡在網(wǎng)站創(chuàng)立初期,我們一般都使用單臺機(jī)器對臺提供集中式服務(wù),但是隨著業(yè)務(wù)量越來越大,無論是性能上還是穩(wěn)定性上都有了更大的挑戰(zhàn)。就鹿晗宣布戀情導(dǎo)致微博宕機(jī)事件淺談大型網(wǎng)站高可用性架構(gòu)中午吃飯刷著刷著微博發(fā)現(xiàn)微博突然掛了。 分布式架構(gòu)實(shí)踐——負(fù)載均衡 在網(wǎng)站創(chuàng)立初期,我們一般都使用單臺機(jī)器對臺提供集中式服務(wù),但是隨著業(yè)務(wù)量越來越大,無論是性能上還是穩(wěn)定性上都有了更大的挑...
摘要:阿里巴巴的共享服務(wù)理念以及企業(yè)級互聯(lián)網(wǎng)架構(gòu)建設(shè)的思路,給這些企業(yè)帶來了不少新的思路,這也是我最終決定寫這本書的最主要原因。盡在雙阿里巴巴技術(shù)演進(jìn)與超越是迄今唯一由阿里巴巴集團(tuán)官方出品全面闡述雙八年以來在技術(shù)和商業(yè)上演進(jìn)和創(chuàng)新歷程的書籍。 showImg(https://segmentfault.com/img/remote/1460000015386860); 1、大型網(wǎng)站技術(shù)架構(gòu):核...
閱讀 1224·2021-11-11 16:54
閱讀 1738·2021-10-13 09:40
閱讀 932·2021-10-08 10:05
閱讀 3497·2021-09-22 15:50
閱讀 3701·2021-09-22 15:41
閱讀 1781·2021-09-22 15:08
閱讀 2338·2021-09-07 10:24
閱讀 3570·2019-08-30 12:52