安装python爬虫scrapy踩过的那些坑和编程外的思考

这些天应朋友的要求抓取某个论坛帖子的信息,网上搜索了一下开源的爬虫资料,看了许多对于开源爬虫的比较发现开源爬虫scrapy比较好用。但是以前一直用的java和php,对python不熟悉,于是花一天时间粗略了解了一遍python的基础知识。于是就开干了,没想到的配置一个运行环境就花了我一天时间。下面记录下安装和配置scrapy踩过的那些坑吧。

运行环境:CentOS 6.0 虚拟机

开始上来先得安装python运行环境。然而我运行了一下python命令,发现已经自带了,窃(大)喜(坑)。于是google搜索了一下安装步骤,pip install Scrapy直接安装,发现不对。少了pip,于是安装pip。再次pip install Scrapy,发现少了python-devel,于是这么来回折腾了一上午。后来下载了scrapy的源码安装,突然曝出一个需要python2.7版本,再通过python --version查看,一个2.6映入眼前;顿时千万个草泥马在心中奔腾。

于是查看了官方文档(http://doc.scrapy.org/en/master/intro/install.html),果然是要python2.7。没办法,只能升级python的版本了。

1、升级python

  • 下载python2.7并安装
#wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
#tar -zxvf Python-2.7.10.tgz
#cd Python-2.7.10
#./configure  –enable-shared CFLAGS=-fPIC
#make all             
#make install  
#make clean  
#make distclean
  • 检查python版本
#python --version

发现还是2.6

  • 更改python命令指向
#mv /usr/bin/python /usr/bin/python2.6.6_bak
#ln -s /usr/local/bin/python2.7 /usr/bin/python
  • 再次检查版本
# python --version
Python 2.7.10

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

安装了python2.7,第一次执行时报错:
error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
 
解决方法如下:
1.编辑      vi /etc/ld.so.conf 
如果是非root权限帐号登录,使用 sudo vi /etc/ld.so.conf 
添加上python2.7的lib库地址,如我的/usr/local/Python2.7/lib,保存文件
 
2.执行 /sbin/ldconfig -v命令,如果是非root权限帐号登录,使用 sudo  /sbin/ldconfig -v。这样 ldd 才能找到这个库,执行python2.7就不会报错了
 
/etc/ld.so.conf:
这个文件记录了编译时使用的动态链接库的路径。
默认情况下,编译器只会使用/lib和/usr/lib这两个目录下的库文件
如果你安装了某些库,没有指定 --prefix=/usr 这样lib库就装到了/usr/local下,而又没有在/etc/ld.so.conf中添加/usr/local/lib,就会报错了
 
ldconfig是个什么东东吧 :
它是一个程序,通常它位于/sbin下,是root用户使用的东东。具体作用及用法可以man ldconfig查到
简单的说,它的作用就是将/etc/ld.so.conf列出的路径下的库文件 缓存到/etc/ld.so.cache 以供使用
因此当安装完一些库文件,(例如刚安装好glib),或者修改ld.so.conf增加新的库路径后,需要运行一下/sbin/ldconfig
使所有的库文件都被缓存到ld.so.cache中,如果没做,即使库文件明明就在/usr/lib下的,也是不会被使用的,结果
编译过程中抱错,缺少xxx库。
 

到这里,python算是升级完成了,继续安装scrapy。于是pip install scrapy,还是报错。

Collecting Twisted>=10.0.0 (from scrapy)
  Could not find a version that satisfies the requirement Twisted>=10.0.0 (from scrapy) (from versions: )
No matching distribution found for Twisted>=10.0.0 (from scrapy)

少了 Twisted,于是安装 Twisted

2、安装Twisted

  • 下载Twisted(https://pypi.python.org/packages/source/T/Twisted/Twisted-15.2.1.tar.bz2#md5=4be066a899c714e18af1ecfcb01cfef7)
  • 安装
cd Twisted-15.2.1
python setup.py install
  • 查看是否安装成功
python
Python 2.7.10 (default, Jun  5 2015, 17:56:24) 
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import twisted
>>>

此时索命twisted已经安装成功。于是继续pip install scrapy,还是报错。

Collecting libxlst
  Could not find a version that satisfies the requirement libxlst (from versions: )
No matching distribution found for libxlst
Collecting libxml2
  Could not find a version that satisfies the requirement libxml2 (from versions: )
No matching distribution found for libxml2
wget http://xmlsoft.org/sources/libxslt-1.1.28.tar.gz
cd libxslt-1.1.28/
./configure
make
make install

安装过程报错如下错:

  /bin/rm: cannot remove `libtoolT': No such file or directory

解决方法:

这时直接打开 configure,把 $RM “$cfgfile” 那行删除掉,重新再运行 ./configure 就可以了。

找到 vi configure  大概在文件的最后部分找到如下: 注释掉  $RM "$cfgfile"  ,或者直接删除也可以, 然后重新运行./configure即可。

   cfgfile="${ofile}T"
    trap "$RM "$cfgfile"; exit 1" 1 2 15
    $RM "$cfgfile"

    cat <<_LT_EOF >> "$cfgfile"
#! $SHELL





wget ftp://xmlsoft.org/libxml2/libxml2-git-snapshot.tar.gz
cd libxml2-2.9.2/
./configure
make
make install

安装好以后继续pip install scrapy,幸运之星仍未降临

4、安装cryptography

Failed building wheel for cryptography

下载cryptography(https://pypi.python.org/packages/source/c/cryptography/cryptography-0.4.tar.gz)

安装

cd cryptography-0.4
python setup.py build
python setup.py install

发现安装的时候报错:

No package 'libffi' found

于是下载libffi下载并安装

wget ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz
cd libffi-3.2.1
make
make install

安装后发现仍然报错

Package libffi was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libffi.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libffi' found

于是设置:PKG_CONFIG_PATH

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

再次安装scrapy

pip install scrapy

幸运女神都去哪儿了?

ImportError: libffi.so.6: cannot open shared object file: No such file or directory

于是

whereis libffi
libffi: /usr/local/lib/libffi.a /usr/local/lib/libffi.la /usr/local/lib/libffi.so

已经正常安装,网上搜索了一通,发现是LD_LIBRARY_PATH没设置,于是

export LD_LIBRARY_PATH=/usr/local/lib

于是继续安装cryptography-0.4

./configure
make
make install

此时正确安装,没有报错信息了。

原文地址:https://www.cnblogs.com/xianguang/p/7019209.html