以下所有的安装都是在Ubunt server 18.04 LTS 64bit服务器版本(命令行无界面版本)下进行,编程语言使用Python3.8.0
sudo apt update
sudo apt install -y libappindicator1 fonts-liberation
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
sudo apt -f install
sudo dpkg --configure -a
sudo apt -u dist-upgrade
sudo apt -y install dbus-x11 xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic xfonts-scalable
// 上面这些命令执行完了后,再次安装,一般能成功了
sudo apt update
sudo apt install -y libappindicator1 fonts-liberation
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb
可以通过查看版本号来确定是否安装成功:
google-chrome --version
或者
google-chrome-stable --version
能够打印出版本号就是安装成功了。
google-chrome-stable --headless --no-sandbox --disable-gpu --dump-dom https://www.chromestatus.com/
// required argument "--no-sandbox" when running as root user. otherwise you would get no sandbox errors.
https://blog.softhints.com/ubuntu-16-04-server-install-headless-google-chrome/
打开网站后往下拉,拉到Solution Install Google Chrome on Ubuntu 16.04 no GUI这个版块
sudo apt update
sudo apt install firefox
selenium想要使用Firefox就必须要安装浏览器驱动 - geckodriver
这里有个导航网站,主流浏览器的驱动都能从它这里找到:https://selenium.dev/selenium/docs/api/py/
*同时,也指出了驱动必须要被正确地放到指定目录下。
尽量到官方网站上去下载驱动,官方网站:https://github.com/mozilla/geckodriver/releases
如果因为不可抗力因素导致官方网站打不开,只能用备用网站了。备用网站:https://npm.taobao.org/mirrors/geckodriver/
写随笔日期是2019年11月24日16:38:39,我下载了最新版的驱动:
可以使用linux命令wget进行下载,也可以下载到本地后通过lrzsz上传到linux中。
解压命令:
sudo tar -xvf geckodriver-v0.26.0-linux64.tar.gz
必须将geckodriver放到系统可执行目录下,/usr/bin/ 或者 /usr/local/bin/
cp geckodriver /usr/bin/
cp geckodriver /usr/local/bin/
Firefox想要使用selenium必须先安装一个系统级的依赖:
sudo apt install Xvfb
然后安装应用级的python依赖:
pip install PyVirtualDisplay
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.add_argument('-headless')
options.add_argument('--no-sandbox')
driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver', options=options)
driver.get("https://www.baidu.com")
print(driver.title)
driver.quit()
最终结果:
原文:https://www.cnblogs.com/quanquan616/p/11922771.html