liunx centos7无GUI服务器跑Selenium部署说明
1.安装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
注意:google-chrome一定一定不要用root用户去运行
2.安装chromedrive
从https://sites.google.com/a/chromium.org/chromedriver/home下载chromedriver
配置在PATH路径或者在脚本中指定路径
wget -N http://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip unzip chromedriver_linux64.zip
chmod +x chromedriver
sudo mv -f chromedriver /usr/local/share/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
3.安装Xvfb
无GUI时,Xvfb为虚拟GUI
yum update
yum install Xvfb
yum -install libXfont
yum install xorg-x11-fonts*
4.安装selenium、pyvirtualdisplay
pip install selenium
pip install pyvirtualdisplay
5.写个小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()
保存文件,执行命令,即可看到效果
python test.py
注意注意注意,root用户会报错 Chrome failed to start: exited abnormally,一定要用其它用户执行