首先安装chrome浏览器,首先可以先建一个文件加用来存放chrome和chrome driver文件。比如我的就在/usr/local/chrome
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate这个命令是安装最新的版本的chrome浏览器,之后运行
yum install ./google-chrome-stable_current_x86_64.rpm注意看清文件名称哦。到这chrome安装好了 下一步安装chrome driver
wget http://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip这个下载的是2.43版本的,可以根据自己chrome的版本选择, 之后解压
unzip chromedriver_linx64.zip #移动到/usr/bin目录下 mv chromedriver /usr/bin/到这就安装好了chrome driver。 selenium安装直接pip install selenium就好了。 下面是测试代码
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() #浏览器不提供可视化界面,Linux系统一定要加这个要不然启动报错 chrome_options.add_argument('--headless') # 以最高权限运行,这个也要加,我的不加会报错 chrome_options.add_argument('--no-sandbox') browser = webdriver.Chrome(chrome_options=chrome_options) browser.get('https://www.baidu.com') print(browser.page_source)在安装过程中 /usr/libexec目录下的 urlgrabber-ext-down ,文件报错,打开文件把第一行的python改成python2就好了(这是在python默认为python3的前提下啊)