centos7装python3,并安装requests, lxml,bs4和phantomjs模块
一. 安装python3并设为默认版本,与python2共存
安装python3.6可能使用的依赖
# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
1、下载Python3.6安装包 解压、编译、安装
新安装的最新centos7最小化安装没有安装wget,所以要安装一下
# yum install wget
# wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
# tar -xzvf Python-3.6.6.tgz
# cd Python-3.6.6
把Python3.6安装到 /usr/local 目录,使用make altinstall,如果使用make install,在系统中将会有两个不同版本的Python在/usr/bin/目录中。这将会导致很多问题
# ./configure --prefix=/usr/local 或者 ./configure --prefix=/usr/local/python3
如果遇到:configure: error: no acceptable C compiler found in $PATH
解决方法:# yum install gcc
# make
# make install 或者 make altinstall
更改/usr/bin/python链接 创建Python3.6的链接,并且设置python3为默认版本(CentOS6.7默认安装的是Python2)
# cd /usr/bin
# mv python python.backup
# ln -s /usr/local/bin/python3.6 /usr/bin/python
# ln -s /usr/local/bin/python3.6 /usr/bin/python3
更改yum脚本的python依赖
# cd /usr/bin
# ls yum*
# vi /usr/bin/yum
# vi /usr/libexec/urlgrabber-ext-down
因为执行yum需要python2版本,所以我们还要修改yum的配置, 更改以上文件头为:
#!/usr/bin/python 改为 #!/usr/bin/python2
之后python3.6就完成了,能够同时使用python3.6和python2.7.
setuptools安装
wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26
tar -zxvf setuptools-19.6.tar.gz
cd setuptools-19.6
sudo python3 setup.py build
sudo python3 setup.py install
pip3安装
wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
sudo python3 setup.py build
sudo python3 setup.py install
sudo ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip
sudo ln -s /usr/local/python3.6/bin/pip3 /usr/bin/pip3
建议把pip和python分别指向pip3和python3!
二. 安装模块
1. 安装bs4
tar xvzf beautifulsoup4-4.5.1.tar.gz
cd beautifulsoup4-4.5.1
python setup.py install
2. 安装requests
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install requests
3. 安装lxml,依赖有libxml2-devel,libxslt-devel
yum install libxml2-devel libxslt-devel -y
pip install lxml
4. 安装phantomjs模块
wgethttps://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
yum install bzip2 # 安装bzip2
tar -jxvf phantomjs-2.1.1-linux-x86_64.tar.bz2
mv phantomjs-2.1.1-linux-x86_64 /usr/local/src/phantomjs
ln -sf /usr/local/src/phantomjs/bin/phantomjs /usr/local/bin/phantomjs
yum install fontconfig freetype2
phantomjs -v # 测试版本号
简单的python selenium 运用
from selenium import webdriver
driver=webdriver.PhantomJS(executable_path='/usr/local/bin/phantomjs')
driver.get('')
driver.find_element_by_id("").clear()
driver.find_element_by_id("").send_keys('')
driver.find_element_by_xpath("").click()