centos7更换为清华repo源官方指导
1.备份默认YUM源文件
# cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2.sed命令编辑新的YUM源文件,\表示分段输入命令
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo
3.生成YUM源缓存 # yum clean all & yum makecache
4.查看当前YUM源 # yum repolist
1.简便安装python3
yum search python3 -y
验证是否安装成功
python3 -v
2.安装最新版本的chrome浏览器
获取最新的chrome包
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
本地安装chrome
yum localinstall -y google-chrome-stable_current_x86_64.rpm
查看安装的chrome版本,以便于后面chromedriver和这个版本对上
google-chrome -version
3.安装chromedriver,去npm镜像网站找到自己chrome版本对应的二进制文件CNPM Binaries Mirror
windows下载到本地后,直接将chromedriver.zip放到自己的centos目录下并解压成chromedriver
将chromedriver移动到bin下, mv chromedriver /usr/bin
赋予777权限chmod 777 chromedriver
chromedriver --version 核对下版本,且是否安装成功
4.安装selenium库,这里使用清华源安装
pip3 install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple
安装成功后运行 pip3 show selenium,查看selenium相关信息
5.编写一个简单的selenium无头无界面的自动化脚本test.py
# 导入 seleniun 相关类
from selenium import webdriver
from selenium.webdriver.common.by import By
#导入浏览器驱动属性设置类
from selenium.webdriver.chrome.options import Options
# 在linux系统中 需要设置一些参数来规避一些信息
chrome_options = Options()
# 设置浏览器的无头浏览模式 无头无界面 那在linux下就可以运行了
chrome_options.add_argument('--headless')
# 避免 DevToolsActivePort 不存在报错的形式
chrome_options.add_argument('--no-sandbox')
# 官方关闭的一些关键选项,规避一些BUG,不使用gpu
chrome_options.add_argument('--disable-gpu')
# 创建一个linux的谷歌浏览器,并将设置的属性传入(windows中直接不用设置这个属性)
test_webdriver = webdriver.Chrome(options=chrome_options)
# 模拟百度打开百度 并进行过搜索
test_webdriver.get('https://www.baidu.com/')
# 模拟输入框输入文字
test_webdriver.find_element_by_xpath('//input[@id="kw"]').send_keys('python')
# 模拟按钮查询
test_webdriver.find_element_by_xpath('//input[@id="su"]').click()
# 打印标题
print(test_webdriver.title)
# 关闭谷歌浏览器
test_webdriver.quit()
运行python3 test.py,如果运行时报错,可以将代码中的注释全部删了试试
删除异常的chrome进程,grep -v表示筛选出去掉当前ps -ef |grep chrome命令进程的其他进程
ps -ef | grep chrome| grep -v grep | awk '{print $2}'| xargs kill -9