由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到
[root@iZm5efjrz9szlsq1a0ai3gZ ~]# python
Python 2.7. (default, Jun , ::)
[GCC 4.8. (Red Hat 4.8.-)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
输入Python命令,查看可以得知是Python2.7.5版本
which python
可以查看位置,一般是位于/usr/bin/python目录下。
下面介绍安装Python3的方法
首先安装依赖包
yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
然后根据自己需求下载不同版本的Python3,我下载的是Python3.6.2
mkdir /usr/local/python3 创建一个文件夹
cd /usr/local/python3 进入文件夹 wgt https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz
建立一个空文件夹
然后解压压缩包,进入该目录,安装Python3
tar -xvJf Python-3.6..tar.xz
cd Python-3.6.
./configure --prefix=/usr/local/python3
make && make install
最后创建软链接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
在命令行中输入python3测试
上面我们已经把python3跟pip3安装好了,接下来,我们进行Python3下的Scrapy框架安装
3 安装Scrapy爬虫框架
(1)安装Scrapy
[root@hadron ~]# pip3 install scrapy
Collecting scrapy
Downloading Scrapy-1.5.-py2.py3-none-any.whl (251kB)
% |████████████████████████████████| 256kB .1MB/s
Requirement already satisfied: lxml in /usr/local/python3/lib/python3./site-packages (from scrapy)
Collecting PyDispatcher>=2.0. (from scrapy)
Downloading PyDispatcher-2.0..tar.gz
....
....
Collecting pycparser (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=2.1.->pyOpenSSL->scrapy)
Downloading pycparser-2.18.tar.gz (245kB)
% |████████████████████████████████| 256kB 339kB/s
Installing collected packages: PyDispatcher, zope.interface, constantly, incremental, six, attrs, Automat, hyperlink, Twisted, cssselect, w3lib, parsel, asn1crypto, pycparser, cffi, cryptography, pyOpenSSL, pyasn1, pyasn1-modules, service-identity, queuelib, scrapy
Running setup.py install for PyDispatcher ... done
Running setup.py install for Twisted ... done
Running setup.py install for pycparser ... done
Successfully installed Automat-0.6. PyDispatcher-2.0. Twisted-17.9. asn1crypto-0.24. attrs-17.4. cffi-1.11. constantly-15.1. cryptography-2.1. cssselect-1.0. hyperlink-18.0. incremental-17.5. parsel-1.4. pyOpenSSL-17.5. pyasn1-0.4. pyasn1-modules-0.2. pycparser-2.18 queuelib-1.4. scrapy-1.5. service-identity-17.0. six-1.11. w3lib-1.19. zope.interface-4.4.
[root@hadron ~]#
在这里进行安装的时候,我们可能会出现一串的报红,一串的报红。
很蛋疼的报红信息
出现报红不要紧,我们多试几次pip3 install scrapy 就行了
(2)网络好的话,试下不超过5次,应该就会出现下面安装成功的界面
成功安装Scrapy的界面
(3)上面的图也是验证Scrapy框架安装成功的界面,因为我们进行了import scrapy没有进行报错。
(4)创建Scrapy软链接
root@hadron ~]# ln -s /usr/local/python3/bin/scrapy /usr/bin/scrapy
(5)验证Scrapy软链接
[root@hadron ~]# scrapy -v
Scrapy 1.5. - no active project
上面就是整个Python3下Scrapy框架的安装步骤过程,基本上没有bug。