[178]centos7服务器无GUI情况下安装使用Xvfb、selenium、chrome和selenium-server

最近需要用到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账号下运行。

也可以通过下载rpm包谷歌文件进行安装
安装命令

yum localinstall *.rpm

查看谷歌版本号google-chrome-stable --version

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
# -*- coding:utf-8 -*-  
from selenium import webdriver  
from pyvirtualdisplay import Display  
  
  
display = Display(visible=0, size=(800,600))  
display.start()  
driver = webdriver.Chrome("./chromedriver")  
driver.get("http://www.baidu.com")  
print driver.page_source 

driver.quit()  
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

# -*- coding:utf-8 -*-  
from selenium import webdriver  
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities  
  
  
driver = webdriver.Remote(  
   command_executor='http://127.0.0.1:4444/wd/hub',  
   desired_capabilities=DesiredCapabilities.CHROME)  
  
driver.get("http://www.baidu.com")  
print driver.page_source  
driver.quit()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值