首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >centos7无GUI情况安装Xvfb、selenium、chrome

centos7无GUI情况安装Xvfb、selenium、chrome

作者头像
拓荒者
发布2019-03-11 11:03:11
发布2019-03-11 11:03:11
3.6K00
代码可运行
举报
文章被收录于专栏:运维经验分享运维经验分享
运行总次数:0
代码可运行

代码语言:javascript
代码运行次数:0
运行
复制
</pre><pre>

最近需要用到selenium浏览器抓取,在windows下对照chrome浏览器开发的代码,在linux服务器上换成phantomjs驱动后,却不能运行了,通过截图发现phantomjs渲染效果和chrome不同。于是考虑在centos上安装chrome浏览器。

下面是做的一些记录。

1,centos7 安装 google-chrome

(1) 添加chrome的repo源

 vi /etc/yum.repos.d/google.repo

[google]name=Google-x86_64baseurl=http://dl.google.com/linux/rpm/stable/x86_64enabled=1gpgcheck=0gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

(2)安装

yum update , 然后yum install google-chrome-stable

这时候在非root账号下运行google-chrome会输出没有显示器,无法启动之类的。

不知道为什么不能在root账号下运行。

2, 安装Xvfb

yum update

yum install Xvfb

yum -install libXfont

yum install xorg-x11-fonts*

3, 一个小测试

(1)安装selenium、pyvirtualdisplay

pip install selenium

pip install pyvirtualdisplay

(2)下载chromedriver

从https://sites.google.com/a/chromium.org/chromedriver/home下载chromedriver

配置在PATH路径或者在脚本中指定路径  

(3) demo

  vim test.py

代码语言:javascript
代码运行次数:0
运行
复制
  1. # -*- coding:utf-8 -*-
代码语言:javascript
代码运行次数:0
运行
复制
  1. from selenium import webdriver
  2. from pyvirtualdisplay import Display
  3. display = Display(visible=0, size=(800,600))
  4. display.start()
  5. driver = webdriver.Chrome("./chromedriver")
  6. driver.get("http://www.baidu.com")
  7. print driver.page_source
代码语言:javascript
代码运行次数:0
运行
复制
  1. driver.quit()
  2. display.stop()

期望输出百度首页的html文档。

4 ,selenium server

1, nohup Xvfb -ac :7 -screen 0 1280x1024x8 > /dev/null 2>&1 &

2, export DISPLAY=:7

3, java -jar selenium-server-standalone-3.0.1.jar

4,

代码语言:javascript
代码运行次数:0
运行
复制
  1. <pre name="code" class="python"># -*- coding:utf-8 -*-
  2. from selenium import webdriver
  3. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  4. driver = webdriver.Remote(
  5. command_executor='http://127.0.0.1:4444/wd/hub',
  6. desired_capabilities=DesiredCapabilities.CHROME)
  7. driver.get("http://www.baidu.com")
  8. print driver.page_source
  9. driver.quit()

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/09/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1,centos7 安装 google-chrome
    • (1) 添加chrome的repo源
    • (2)安装
  • 2, 安装Xvfb
  • 3, 一个小测试
  • 4 ,selenium server
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档