Scrapy是一个开源的机遇twisted框架的python的单机爬虫,该爬虫实际上包含大多数网页抓取的工具包,用于爬虫下载端以及抽取端。

安装环境:

  1. centos5.4  
  2. python2.7.3 

安装步骤:

1.下载python2.7  http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz

  1. [root@zxy-websgs ~]# wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz -P /opt  
  2. [root@zxy-websgs opt]# tar xvf Python-2.7.3.tgz 
  3. [root@zxy-websgs Python-2.7.3]# ./configure 
  4. [root@zxy-websgs Python-2.7.3]# make && make install 

 验证python2.7安装

  1. [root@zxy-websgs Python-2.7.3]# python2.7 
  2. Python 2.7.3 (default, Feb 28 2013, 03:08:43)   
  3. [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2 Type "help", "copyright",
  4. "credits" or "license" for more information.  
  5. >>> exit() 

2.安装setuptools

  1. [root@zxy-websgs ~]# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz -P /opt/  
  2. [root@zxy-websgs opt]# tar zxvf setuptools-0.6c11.tar.gz   
  3. [root@zxy-websgs setuptools-0.6c11]# python2.7 setup.py  install 

setuptools:http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

3.安装Twisted

  1. [root@zxy-websgs setuptools-0.6c11]# easy_install Twisted  
  2. ......  
  3. Installed /usr/local/lib/python2.7/site-packages/Twisted-12.3.0-py2.7-linux-x86_64.egg  
  4. ......  
  5. Installed /usr/local/lib/python2.7/site-packages/zope.interface-4.0.4-py2.7-linux-x86_64.egg 

Twisted要安装zope.interface,可以从下面地址下载

zope.interface:http://pypi.python.org/packages/source/z/zope.interface/zope.interface-4.0.1.tar.gz

twisted:http://twistedmatrix.com/Releases/Twisted/12.1/Twisted-12.1.0.tar.bz2

5.安装w3lib

  1. [root@zxy-websgs setuptools-0.6c11]# easy_install -U w3lib  
  2. Searching for w3lib Reading http://pypi.python.org/simple/w3lib/  
  3. Reading http://github.com/scrapy/w3lib Best match: w3lib 1.2  
  4. Downloading http://pypi.python.org/packages/source/w/w3lib/w3lib-1.2.tar.gz#md5=f929d5973a9fda59587b09a72f185a9e 
  5. Processing w3lib-1.2.tar.gz 
  6. Running w3lib-1.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-wm_1BB/w3lib-1.2/egg-dist-tmp-2DQHY_ zip_safe flag not set;  
  7. analyzing archive contents... Adding w3lib 1.2 to easy-install.pth file   
  8. Installed /usr/local/lib/python2.7/site-packages/w3lib-1.2-py2.7.egg  
  9. Processing dependencies for w3lib Finished processing dependencies for w3lib 

w3lib:http://pypi.python.org/packages/source/w/w3lib/w3lib-1.2.tar.gz

6.安装libxml2或者用easy_install安装lxml

  1. [root@zxy-websgs lxml-3.1.0]# easy_install lxml 

验证lxml安装

  1. [root@zxy-websgs lxml-3.1.0]# python2.7  
  2. Python 2.7.3 (default, Feb 28 2013, 03:08:43)   
  3. [GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2 Type "help", "copyright", "credits" or "license" for more information. 
  4. >>> import lxml 
  5. >>> exit() 

也可以安装libxml2,官网上推荐安装2.6.28或者以上的版本,但在官网上没找到,我先是安装的2.6.9的版本,运行scrapy时报以下错误

  1. Traceback (most recent call last): 
  2.   File "/usr/local/bin/scrapy", line 5, in <module> 
  3.     pkg_resources.run_script('Scrapy==0.14.4', 'scrapy') 
  4.   File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 489, in run_script 
  5.   File "build/bdist.linux-x86_64/egg/pkg_resources.py", line 1207, in run_script 
  6.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/EGG-INFO/scripts/scrapy", line 4, in <module> 
  7.     execute() 
  8.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/cmdline.py", line 112, in execute 
  9.     cmds = _get_commands_dict(inproject) 
  10.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/cmdline.py", line 37, in _get_commands_dict 
  11.     cmds = _get_commands_from_module('scrapy.commands', inproject) 
  12.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/cmdline.py", line 30, in _get_commands_from_module 
  13.     for cmd in _iter_command_classes(module): 
  14.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/cmdline.py", line 21, in _iter_command_classes 
  15.     for module in walk_modules(module_name): 
  16.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/utils/misc.py", line 65, in walk_modules 
  17.     submod = __import__(fullpath, {}, {}, ['']) 
  18.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/commands/shell.py", line 8, in <module> 
  19.     from scrapy.shell import Shell 
  20.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/shell.py", line 14, in <module> 
  21.     from scrapy.selector import XPathSelector, XmlXPathSelector, HtmlXPathSelector 
  22.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/selector/__init__.py", line 30, in <module> 
  23.     from scrapy.selector.libxml2sel import * 
  24.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/selector/libxml2sel.py", line 12, in <module> 
  25.     from .factories import xmlDoc_from_html, xmlDoc_from_xml 
  26.   File "/usr/local/lib/python2.7/site-packages/Scrapy-0.14.4-py2.7.egg/scrapy/selector/factories.py", line 14, in <module> 
  27.     libxml2.HTML_PARSE_NOERROR + \ 
  28. AttributeError: 'module' object has no attribute 'HTML_PARSE_RECOVER' 

升级到2.6.21版本以后解决了。

libxml2.6.1:ftp://xmlsoft.org/libxml2/python/libxml2-python-2.6.21.tar.gz

7.安装pyOpenSSL(这个是可选安装的,主要为了使scrapy能够支持https)

用easy_install pyOpenSSL安装的是pyOpenSSL-0.13版本,没安装成功,于是手动下载.011版本来进行安装。

  1. [root@zxy-websgs opt]# wget http://launchpadlibrarian.net/58498441/pyOpenSSL-0.11.tar.gz -P /opt 
  2. [root@zxy-websgs opt]# tar zxvf pyOpenSSL-0.11.tar.gz  
  3. [root@zxy-websgs pyOpenSSL-0.11]# python2.7 setup.py install 

pyOpenSSL:http://launchpadlibrarian.net/58498441/pyOpenSSL-0.11.tar.gz

8.安装scrapy

  1. [root@zxy-websgs pyOpenSSL-0.11]# easy_install -U Scrapy 

验证安装

  1. [root@zxy-websgs pyOpenSSL-0.11]# scrapy 
  2. Scrapy 0.16.4 - no active project 
  3.  
  4. Usage: 
  5.   scrapy <command> [options] [args] 
  6.  
  7. Available commands: 
  8.   fetch         Fetch a URL using the Scrapy downloader 
  9.   runspider     Run a self-contained spider (without creating a project) 
  10.   settings      Get settings values 
  11.   shell         Interactive scraping console 
  12.   startproject  Create new project 
  13.   version       Print Scrapy version 
  14.   view          Open URL in browser, as seen by Scrapy 
  15.  
  16.   [ more ]      More commands available when run from project directory 
  17.  
  18. Use "scrapy <command> -h" to see more info about a command 

scrapy:http://pypi.python.org/packages/source/S/Scrapy/Scrapy-0.14.4.tar.gz

总结:

pyOpenSSL单独安装的时候不成功,也可以先下载pyOpenSSL0.11进行安装,再使用easy_install -U Scrapy进行全程安装