Centos7.5 服务器搭建Selenium 爬虫详细教程
测试成功时间2020.03.12
1.安装python
我安装的是python3.6
需要给服务器安装Python环境
最新的EPEL 7仓库提供了Python3(python 3.6)的安装源,如果你使用CentOS7或更新的版本的系统你也可以按照下面的步骤很轻松的从EPEL仓库安装。
安装最新版本的EPEL
sudo yum install epel-release
用yum安装python 3.6:
sudo yum install python36
注意:上面的安装方法并未安装pip和setuptools,如果你要安装这两个库可以使用下面的命令:
curl -O https://bootstrap.pypa.io/get-pip.py
sudo /usr/bin/python3.6 get-pip.py
安装python的库,这里使用国内的清华镜像源快速安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple selenium
2.安装Chrome浏览器
2.1.安装chrome浏览器,创建yum源文件
cd /etc/yum.repos.d/
touch google-chrome.repo
vi google-chrome.repo
2.2 输入yum源信息
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
2.3 安装google chrome和chromedriver
yum -y install google-chrome-stable --nogpgcheck
yum install chromedriver
3.测试代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
opt = Options()
opt.set_headless()
opt.add_argument('--no-sandbox')
opt.add_argument('--disable-dev-shm-usage')
opt.add_argument('--headless')
opt.add_argument('blink-settings=imagesEnabled=false')
opt.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=opt,executable_path="/usr/bin/chromedriver")
driver.get('http://www.baidu.com')
测试成功
4.一些注意点
如果出现这样的情况,那么你缺少基本的这些配置代码
opt = Options()
opt.set_headless()
opt.add_argument('--no-sandbox')
opt.add_argument('--disable-dev-shm-usage')
opt.add_argument('--headless')
opt.add_argument('blink-settings=imagesEnabled=false')
opt.add_argument('--disable-gpu')
另外chromedriver的路径和版本问题需要注意