scrapy的安裝
場景描述
最近公司業務不是很忙,想從網上找點資料出來,用來.....(你懂得,,,別想歪,我是正經人!)
由于本人是做java的,之前一直在用jsoup來玩爬蟲,聽說python的scrapy爬蟲簡直就是搜易賊(so easy)。
哪就走起吧.
配置狀況
我用的mac版本是OS X EI capitan。
問題描述
本機自帶了python2.7,直接安裝scrapy就可以了,
不過在安裝scrapy之前要先確定你的電腦是否已經安裝了pip。
如果沒安裝pip的話,打開終端(我用的itrem2),執行以下的命令
sudo easy_install pip
pip 和 easy_install 都是 Python 的框架管理命令,pip 是對 easy_install的升級。
安裝完pip
之后我們要開始安裝scrapy了,打開終端執行
sudo pip install Scrapy
如果執行成功,那么 Scrapy 就安裝成功了,但往往事與愿違,你很有可能遇到如下錯誤:
OSError: [Errno 1] Operation not permitted: '/tmp/pip-Tz8iWw-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
故障定位
我google了好久,查了好多原因,試了很多種辦法發現都沒說道點子上,最后在以為大神的博客里找到了原因
Because six ships with the system, and almost every popular python project uses it for forwards compatibility, pip tries to upgrade the version it finds first in the python path. Since SIP blocks this, it fails.
Any python dependencies system software has should be hard-coded, and the default path should look in /Library/Python/2.7/site-packages first in order.
原文傳送門
這時候,我們知道了新版的mac系統增加了sip特性,即使使用 sudo 也無法使獲得最高權限,無法對 MAC 系統級的目錄進行更改
解決問題
既然我們已經發現問題出現在sip上了,那我們把sip特性關閉了不就完了么,那么我們怎么關閉sip特性呢。
- 重啟 MAC ,在重啟的過程中按住 Command+R,進入安全模式
- 在頂部的菜單欄中打開終端 ,輸入 csrutil disable 命令關閉 SIP 安全特性(想要在開啟sip的話就用csrutil enable命令即可)
- 重啟 MAC就ok了
此時,sip特性已經被我們關閉了,你可以重新安裝scrapy試試,打開終端執行
sudo pip install Scrapy
在短暫的安裝過程等待過后,你原本期望的是安裝成功的提示,但是你發現安裝又tm的失敗了,fuck。
這時候又發送什么原因了呢,看來下控制臺,你發現了如下的錯誤
Scrapy throws ImportError: cannot import name xmlrpc_client
這貨又是什么梗?
于是乎,又google了下,發現是six的版本太低了原文傳送門
那我們更新下six的版本吧,打開控制臺輸入以下的命令
sudo rm -rf /Library/Python/2.7/site-packages/six*
sudo rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six*
sudo pip install six
ok,我們把six的版本也更新完了,哪這時候我們再試下安裝scrapy把.
這時候會提示你installation successful
,那就恭喜你成功的解決了sip,并安裝了scrapy.
python3的安裝
需求描述
大家應該都知道MAC OS X EI Capitan 系統 支持 python的多版本共存,即在我們的環境變量中可以配置python2和python3。
安裝過程
先安裝python3,如果你安裝了homebrew,那么你只需要輸入一條命令
brew install python3
即可安裝python3此時你可以輸入python3試試,但是你發現這時候系統會提示命令找不到(你都沒配置python3,系統找個毛啊)
這時候我們進行配置了,首先把安裝好的 Python3 目錄移到原本系統所持有的目錄位置,在終端輸入以下命令:
sudo mv /usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5 /System/Library/Frameworks/Python.framework/Versions
注意:/usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5
是你的python3的安裝路徑,一般用homebrew安裝的會是這個路徑,如果你從官網下載安裝的不一定是這個路徑,具體路徑請參考你的python3的安裝路徑然后修改文件所屬的 Group 設置 Group 為 wheel,在終端內輸入以下命令
sudo chown -R root:wheel /usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5
重新鏈接可執行文件
sudo ln -s /usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/bin/pydoc3.5 /usr/bin/pydoc3
sudo ln -s //usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /usr/bin/python3
sudo ln -s /usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/bin/pythonw3.5 /usr/bin/pythonw3
sudo ln -s /usr/local/Cellar/python3/3.5.0/Frameworks/Python.framework/Versions/3.5/bin/python3.5m-config /usr/bin/python3-config
查看結果
在終端里輸入 python3 ,會出現如下提示,說明配置成功:
Python 3.5.0 (default, Sep 23 2015, 04:41:38)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0
(clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or
"license" for more information.