在Python3中安裝Scrapy及常見錯誤
本文整理來自韋蘇恩老師的課程《Python大型網路爬蟲專案開發實戰》
Scrapy要求安裝python3.x版本,我安裝了3.5版本。
-1、安裝python3.x
0、安裝pip一路cd索引到easy_install.exe所在的地方,執行指令“easy_install.exe pip”,安裝完成後輸入pip命令驗證是否安裝成功。
記得新增python.exe pip.exe到環境變數。
1、要求升級pip為最新版
網路安裝,cmd: python -m pip install --upgrade pip
2、升級wheel
網路安裝,cmd: pip install wheel。因為後面要安裝其他軟體包的whl安裝包
3、安裝vspro版
4、安裝lxml
本地安裝,進入到whl檔案的目錄,然後cmd: pip install lxml....whl
5、安裝twisted
本地安裝,進入到whl檔案的目錄,然後cmd: pip install twisted....whl
6、安裝scrapy
網路安裝,cmd: pip install scrapy,也可指定版本,比如: pip install scrapy==1.1.0rc3
7、驗證scrapy是否安裝成功
cmd: scrapy -h
Scrapy常用命令
分兩種:全域性命令和專案命令
全域性命令就是上面cmd: scrapy -h返回的命令,常用的有runspider, startproject
執行爬蟲檔案cmd: scrapy runspider abc.py
不顯示log: --nolog
建立爬蟲專案cmd: scrapy startproject projectname
進入爬蟲專案cmd: cd .\projectname\
輸入cmd: scrapy -h 返回的資訊包含的命令就是專案命令
cmd: scrapy bench檢視硬體環境,我的PC每分鐘大約爬2000多個網頁
cmd: scrapy genspider 建立爬蟲檔案
cmd: scrapy genspider -l 檢視爬蟲檔案模板: basic crawl csvfeed xmlfeed,我們一般用basic
建立爬蟲檔案爬取百度網頁:cmd: scrapy genspider -t basic fileBaidu baidu.com
檢查爬蟲檔案是否建立成功:cmd: scrapy check fileBaidu
執行爬蟲檔案cmd: scrapy crawl fileBaidu
列出當前專案中有的爬蟲檔案cmd: scrapy list
用編輯器開啟爬蟲檔案cmd: scrapy edit fileBaidu (windows 有問題)
獲取URL網址,並指用對應爬蟲檔案進行處理和分析 cmd: scrapy parse http://www.baidu.com,不常用
Scrapy爬蟲專案的建立及爬蟲的建立
我運行了爬百度標題的指令碼,理論上不會有問題,但因為我的電腦之前編碼方式改成了850拉丁文,所以在獲取標題後編碼處理上報了錯,把電腦編碼方式改回936中文即可。