一:安裝chrome (以下是默認下載最新版)
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
查看版本:google-chrome --version
二:安裝chromedriver
- 根據上一步安裝的chrome,通過查看chrome版本,去查找相對應的chromedriver版本。
參考地址:http://chromedriver.chromium.org/downloads
這裏有詳細的chrome和chromedriver 版本對應關係。
比如此刻找到的對應版本的下載連接爲https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
在centos上下載該zip文件。
wget https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
三:添加Chromedriver 軟鏈接
- 比如Chromedriver的位置爲 /root/chromedriver
ln -s /root/chromedriver /usr/bin/chromedriver
安裝完成後,可保存以下py文件進行測試
# -*- coding:utf-8 -*-
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 確保無頭
options.add_argument('--disable-gpu') # 無需要gpu加速
options.add_argument('--no-sandbox') # 無沙箱
driver = webdriver.Chrome(executable_path="/root/chromedriver", chrome_options=options) # 添加軟鏈接後是不需要寫路徑的
driver.get("https://www.baidu.com")
print(driver.page_source)
driver.quit()