按照官方文檔的說明,安裝scrapy 需要以下程序或者庫:
- Python 2.7
- lxml. Most Linux distributions ships prepackaged versions of lxml. Otherwise refer tohttp://lxml.de/installation.html
- OpenSSL. This comes preinstalled in all operating systems except Windows (see Platform specific installation notes)
- pip or easy_install Python package managers
Ubuntu 14.04 已經自帶了前三者。通過以下命令可以驗證:
- 查看python 版本: python -V
qidong@qidong-Vostro-1400:~/Pictures$ python -V
Python 2.7.6
- 查看是否安裝了lxml 和 OpenSSL 庫:
qidong@qidong-Vostro-1400:~/Pictures$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
>>> import OpenSSL
>>>
如果沒有報錯,那就說明已經有這兩個庫了。如果沒有,可以用第四個工具--pip 下載和安裝。
- 安裝pip:
在Ubuntu software center中搜索:easy_install .搜索結果的第二個就是。至於為什么不是easy_install ,我也沒有搞明白 -_-||.如圖示:
安裝好pip之后,我們就可以開始安裝scrapy了。 官方文檔說了,不要使用ubuntu 源里面python-scrapy, 因為那個可能很舊了。
- 安裝scrapy
在終端輸入:sudo pip install Scrapy , 注意S 是大寫的。然后pip 就會自動安裝了(注意要添加sudo 命令,否則就會報寫入被禁止的錯誤)。
但是在安裝的過程中,出現了一個錯誤:
twisted/runner/portmap.c:10:20: fatal error: Python.h No such file or directory.
根據網上的說法,這個頭文件是被gcc 使用,用於編譯其他應用(在這里,我猜測應該是twisted)。 我們需要安裝一個叫 python-dev的包。
於是,我們在終端輸入: sudo apt-get install python-dev .
安裝結束之后,再次輸入 sudo pip install Scrapy 。 就可以正常安裝scrapy了。
安裝結束之后,在終端輸入 scrapy 來驗證我們是否安裝成功並且能否正常運行。 答案是不能。
提示: UserWarning: You do not have the service_identity module installed 。
這個簡單, 在終端輸入 sudo pip install service_identity . 等它安裝結束之后,就OK 了。
再次在終端輸入: scrapy startproject sinaweibo 來創建一個工程來驗證是否安裝成功。 這次成功了!
tips: pip 是個很有用的工具,python使用的包基本都可以用這個工具來安裝。