1. 安装python
2. 安装selenium
pip3 install selenium
因为centos7 会自带python2 ,所以执行python文件的时候需要: python3 ./ xxx.py
3. 安装Chrome
指定yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 下载并安装 curl https://intoli.com/install-google-chrome.sh | bash ldd /opt/google/chrome/chrome | grep "not found" 测试浏览器 google-chrome-stable --no-sandbox --headless --disable-gpu --screenshot https://www.baidu.com/
4. 安装ChromeDriver
查看版本号(注意下载对应版本号的驱动) google-chrome --version 下载安装包 wget https://npm.taobao.org/mirrors/chromedriver/72.0.3626.7/chromedriver_linux64.zip 解压安装包 unzip chromedriver_linux64.zip 将可执行文件移到bin目录 (Mac环境改为 /usr/local/bin 即可) mv chromedriver /usr/bin/
5. 测试:
from selenium import webdriver url = "https://www.baidu.com/" option = webdriver.ChromeOptions() option.add_argument('headless') # 设置无界面 option.add_argument('no-sandbox') # root用户下运行代码需添加这一行 option.add_argument('disable-dev-shm-usage') option.add_experimental_option('useAutomationExtension', False) option.add_experimental_option('excludeSwitches', ['enable-automation']) bro = webdriver.Chrome(chrome_options=option) bro.get(url=url) print("ok")
http://chromedriver.storage.googleapis.com/index.html