1、下载安装chrome浏览器,headless版本
sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb # Might show "errors", fixed by next line
sudo apt-get install -f
2、启动chrome,要打开服务器的9222端口,我使用的是阿里云,需要在安全组里添加
google-chrome --headless --remote-debugging-port=9222 https://chromium.org --disable-gpu --no-sandbox
3、查看chrome版本
google-chrome --version
4、查看对应版本的chromedriver
https://blog.csdn.net/BinGISer/article/details/88559532 (侵删)
5、下载对应版本的chromedriver,在http://chromedriver.storage.googleapis.com/index.html选择对应的版本,使用wget下载
wget https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
6、解压chromedriver_linux64.zip到/usr/bin,打开9515端口
unzip chromedriver_linux64.zip /usr/bin
7、查看chromedriver版本
chromedriver --version
8、安装libgconf2-4
apt -y install libgconf2-4
9、测试
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get("https://www.baidu.com")
print(browser.page_source)
browser.quit()
如果出现错误,一般是chrome版本与chromedriver版本对应不上。