linux 环境下 Java 运行 selenium 无界面 chrome 环境设置

前言

《Java 中使用 selenium 和 chrome 浏览器下载动态网页》 一文中,演示了如何在 window 环境下通过 selenium 和 chrome 来下载动态网页。但是我们的爬虫一般是运行在 linux 服务器上的。服务器上一般是没有 GUI 环境的。无法打开 chrome 窗口界面。以前的时候,爬虫系统是用一种无界面的浏览器 PhantomJS 来实现。但是现在因为 FireFox 、chrome 这些浏览器开始支持无头模式后, PhantomJS 已经停止更新了,所以现在推荐使用 FireFox 和 chrome 的无头模式来替代 PhantomJS 了。所谓的无头模式就是无界面运行模式,正好适合在 linux 服务器这种没有 GUI 环境的情况下使用。

在 linux 环境下使用 selenium 驱动 chrome 需要安装 chrome 浏览器和 chrome webdriver。下面在 centos 7 环境下展示应该如何做。

安装 google chrome

首先从地址 https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm 下载离线安装包。然后执行下面的命令安装 chrome 需要的依赖包

yum install libX11 libXcursor libXdamage libXext libXcomposite libXi libXrandr gtk3 libappindicator-gtk3 xdg-utils libXScrnSaver liberation-fonts

然后执行命令安装 chrome

rpm -ivh google-chrome-stable_current_x86_64.rpm

完成后执行下面的命令查看版本

[root@localhost ~]# google-chrome --version
Google Chrome 70.0.3538.110 

可以看出当前版本为 70 版本

安装 chrome webdriver

和文章 《Java 中使用 selenium 和 chrome 浏览器下载动态网页》 中一样,找到支持 70 版本 chrome 的下载地址,下载 linux 平台版本的文件 chromedriver_linux64.zip 即可

web driver 不同平台

无头模式 selenium chrome 调用程序样例

还是以 《Java 中使用 selenium 和 chrome 浏览器下载动态网页》 中的程序为基础,将他改造为无头模式

        WebDriver webDriver = null;
        try {
            String url = "https://www.jianshu.com/p/675ea919230e";
            ChromeOptions chromeOptions=new ChromeOptions();
            //设置 chrome 的无头模式
            chromeOptions.setHeadless(Boolean.TRUE);
             //启动一个 chrome 实例
            webDriver = new ChromeDriver(chromeOptions);
            //访问网址
            webDriver.get(url);
            Document document = Jsoup.parse(webDriver.getPageSource());
            Element titleElement = document.selectFirst("div.article h1.title");
            Element authorElement = document.selectFirst("div.article div.author span.name");
            Element timeElement = document.selectFirst("div.article span.publish-time");
            Element wordCountElement = document.selectFirst("div.article span.wordage");
            Element viewCountElement = document.selectFirst("div.article span.views-count");
            Element commentCountElement = document.selectFirst("div.article span.comments-count");
            Element likeCountElement = document.selectFirst("div.article span.likes-count");
            Element contentElement = document.selectFirst("div.article div.show-content");
            if (titleElement != null) {
                System.out.println("标题:" + titleElement.text());
            }
            if (authorElement != null) {
                System.out.println("作者:" + authorElement.text());
            }
            if (timeElement != null) {
                System.out.println("发布时间:" + timeElement.text());
            }
            if (wordCountElement != null) {
                System.out.println(wordCountElement.text());
            }
            if (viewCountElement != null) {
                System.out.println(viewCountElement.text());
            }
            if (commentCountElement != null) {
                System.out.println(commentCountElement.text());
            }
            if (likeCountElement != null) {
                System.out.println(likeCountElement.text());
            }

            if (contentElement != null && contentElement.text() != null) {
                System.out.println("正文长度:" + contentElement.text().length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (webDriver != null) {
                //退出 chrome
                webDriver.quit();
            }
        }

和前文中的代码相比,就是下面的地方不一样

            ChromeOptions chromeOptions=new ChromeOptions();
            //设置 chrome 的无头模式
            chromeOptions.setHeadless(Boolean.TRUE);
            //启动一个 chrome 实例
            webDriver = new ChromeDriver(chromeOptions);

这个参数决定了是否用无头模式来启动
将程序打包并且上传到 linux 服务器上,执行命令

java -jar -Dwebdriver.chrome.driver=/data/deploy/chromedriver spider_demo-0.0.1-SNAPSHOT.jar 

控制台会打印出下面的内容

标题:是什么支撑了淘宝双十一,没错就是它java编程语言。
作者:Java帮帮
发布时间:2018.08.29 14:49
字数 561
阅读 632
评论 0
喜欢 4
正文长度:655

说明在 linux 用 java 通过 selenium 调用 chrome 访问这个网页成功了。如果没有设置上面的无头模式参数,那么执行的时候将会出现下面的提示

org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.44.609551 (5d576e9a44fe4c5b6a07e568f1ebc753f1214634),platform=Linux 3.10.0-514.26.2.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 399 milliseconds
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'iz2ze9kvzy03hms75m3jzlz', ip: '172.17.251.3', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-514.26.2.el7.x86_64', java.version: '1.8.0_171'
Driver info: driver.version: ChromeDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
        at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0(JsonWireProtocolResponse.java:53)
        at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2(JsonWireProtocolResponse.java:91)
        at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)
        at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
        at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
        at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
        at com.yanggaochao.spider.SpiderDemoApplication.run(SpiderDemoApplication.java:34)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
        at com.yanggaochao.spider.SpiderDemoApplication.main(SpiderDemoApplication.java:21)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

这样,我们就可以在我们的爬虫系统里面采用浏览器的方式下载网页,实现所见即所得的下载效果。再也不用担心动态渲染的网页内容无法下载了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容