一、进入网页
https://googlechromelabs.github.io/chrome-for-testing/
找到chrome-headless-shell linux64以及chromedriver linux64

二、下载clhrome-headless-shel并解压
1 2
| wget https: unzip chrome-headless-shell-linux64.zip
|
三、移动到系统路径(例如 /opt)
1
| sudo mv chrome-headless-shell-linux64 /opt/chrome-headless-shell
|
四、下载匹配的 chromedriver
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #下载 wget https: #解压 unzip chromedriver-linux64.zip #打开 cd chromedriver-linux64 #移动 sudo mv chromedriver /usr/local/bin/ #给予权限 sudo chmod +x /usr/local/bin/chromedriver #返回上一级 cd .. #删除chromedriver-linux64 rm -rf chromedriver-linux64
|
三、安装依赖库
如果报错缺少库(如 libnss3),安装基础依赖
1 2
| sudo yum install -y atk gtk3 cups-libs libXcomposite libXcursor libXdamage libXext libXi libXtst libxkbcommon pango gtk3 nss alsa-lib pip3 install selenium
|
四、编写 Python 脚本
1 2 3 4 5 6 7 8 9 10 11 12 13
| from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.binary_location = '/opt/chrome-headless-shell/chrome-headless-shell' driver = webdriver.Chrome( executable_path='/usr/local/bin/chromedriver', options=options ) driver.get('https://www.baidu.com') print(driver.title) driver.quit()
|
六、执行
