前言
环境:centos7, python3
一、安装chrome
1.配置yum源
在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo
[google]
name=google
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、安装chrome
yum -y install google-chrome-stable --nogpgcheck
3.建立软连接
找到路径:
which google-chrome-stable
建立软连接
ln -s 找到的路径 /bin/chrome
二、配置selenium
1、安装selenium
pip3 install selenium
2、安装chromedrive
- 1)chromedriver版本地址:在这个网址找到自己需要的chromedriver版本
http://chromedriver.storage.googleapis.com/index.html
- 2)下载,命令如
wget http://chromedriver.storage.googleapis.com/80.0.3987.16/chromedriver_linux64.zip
- 3)解压
unzip chromedriver_linux64.zip
- 4)给予执行权限
chmod +x chromedriver
- 5)脚本中指定路径即可调用chromedriver,
# -*- coding: utf-8 -*-
import time
from selenium import webdriver
driver_path = "/data/selenium/chromedriver"
# 创建chrome参数对象
opt = webdriver.ChromeOptions()
# 把chrome设置成为无界面模式
opt.add_argument('--headless')
# 创建chrome无界面对象
driver = webdriver.Chrome(options=opt, executable_path=driver_path)
driver.implicitly_wait(100)
time.sleep(2)
# 打开浏览器,模拟浏览器请求页面
driver.get('https://www.baidu.com')
# 睡眠3秒
time.sleep(3)
# 关闭浏览器
driver.quit()
cookie_use_times = time.time()