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,一定要用其它用戶執行
liunx centos7無GUI服務器跑Selenium部署說明