1. 查看当前python版本
root@iZwz99sau950q2nhb3pn0aZ ~]# python Python 2.7.5 (default, Aug 7 2019, 00:51:29) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
可以看到执行python,默认是2.7
2 安装python (可百度)
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
编译python源码时,需要一些依赖包,一次安装完毕
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
# 解压压缩包 tar -zxvf Python-3.8.1.tgz # 进入文件夹 cd Python-3.8.1 # 配置安装位置 ./configure prefix=/usr/local/python3 # 安装 make && make install
如果最后没提示出错,就代表正确安装了,在/usr/local/目录下就会有python3目录
root@iZwz99sau950q2nhb3pn0aZ local]# cd /usr/local/ [root@iZwz99sau950q2nhb3pn0aZ local]# ls aegis bin etc games include lib lib64 libexec python3 sbin share src
6. 添加软连接
#添加python3的软链接 ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3 #添加 pip3 的软链接 ln -s /usr/local/python3/bin/pip3.8 /usr/bin/pip3
好了,我们来测试一下python3
[root@iZwz99sau950q2nhb3pn0aZ local]# python3 Python 3.8.1 (default, Feb 4 2020, 11:28:31) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
言归正转进入正题
2.chrome安装和chromedriver下载
1 chrome下载安装
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
查看 google 浏览器版本 google-chrome --version
如果安装出错可以尝试一下方法
cd /etc/yum.repos.d
vim google-chrome.repo
[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
执行
yum -y install google-chrome-stable --nogpgcheck
查看 google 浏览器版本 google-chrome --version
2 chromedriver下载,我下载的和版本对应的
drive镜像网站 https://npm.taobao.org/mirrors/chromedriver/
如果 没有一样的版本号按距离版本号最近的下载
下载下来的包 unzip xxx.zip
3 chromedriver下载后拷贝,到/usr/local/bin/目录下,并且赋可执行的权限:
chmod -R 744 /usr/local/bin/chromedriver
不然在运行程序或或者重启程序时,容易报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
pip3 install selenium
test.py
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument(' headless') options.add_argument(' no-sandbox') driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver',options=options) driver.get("https://www.baidu.com") print(driver.page_source) print(driver.title) driver.close()