安装最新的 Google Chrome
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
安装必要的库
yum install -y epel-release && yum -y update
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts
查看谷歌浏览器版本
google-chrome --version
安装 chromedriver
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
mv chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
加入path
vim .bashrc
在里面加入:
export PATH="$PATH:/usr/bin/chromedriver"
source ~/.bashrc
查看path
$PATH
代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox') # 参数是让Chrome在root权限下跑
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--headless') # 参数是不用打开图形界面
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.page_source)