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
#添加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. >>>
1 chrome下载安装
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
查看 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()
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
编译python源码时,需要一些依赖包,一次安装完毕
yum install wget
这个包是为了下载python源码用的
wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
我是下载的最新的python3.8,如果想安装其他版本,去python官网下载页面下载对应的版本即可。
但是这个下载链接比较慢,我是用迅雷下载到本地之后,再scp到服务器的。
# 解压压缩包 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
#添加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. >>>
这里我没有链接到python上,是因为yum
要用到python2
才能执行,所以现在输入python的话还是会进入python2.7,输入python3才会进入python3.8
如果执意想要链接到python的话,就得修改一下yum的配置:
vi /usr/bin/yum 把 #! /usr/bin/python 修改为 #! /usr/bin/python2 vi /usr/libexec/urlgrabber-ext-down 把 #! /usr/bin/python 修改为 #! /usr/bin/python2
2.chrome安装和chromedriver下载
chrome下载安装
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
查看 google 浏览器版本 google-chrome --version
chromedriver下载,我下载的和版本对应的
drive镜像网站 https://npm.taobao.org/mirrors/chromedriver/
下载下来的包 unzip xxx.zip
chromedriver下载后拷贝,到/usr/local/bin/目录下,并且赋可执行的权限:
chmod -R 744 /usr/local/bin/chromedriver
不然在运行程序或或者重启程序时,容易报错:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH
3.安装selenium,使用是的python3
pip3 install selenium
测试开始:
创建一个名字为test目录,目录结构如下:
[root@localhost test]# tree
├── chromedriver
└── test.py
test.py测试代码如下:chrome界面浏览
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()
测试运行看看
python test.py
一般不会有任何问题
借鉴https://blog.csdn.net/lizhou828/article/details/102954993
分类: 服务器 好文要顶 关注我 收藏该文 .未央