PySpider簡易教程

一、安裝

1、上帝說要有Python

python首先要保證有pip,注意 pip is already installed if you're using Python 2 >=2.7.9 or Python 3 >=3.4

2、安裝Pyspider

然后打開命令行,pip install pyspider
報(bào)錯(cuò)如下:

error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat).
Get it from http://aka.ms/vcpython27

到對應(yīng)地址下載Microsoft Visual C++ Compiler for Python 2.7
繼續(xù)報(bào)錯(cuò):

c:\users\xxx\appdata\local\temp\xmlXPathInitzbzwjw.c(1) : fatal error C1
083: Cannot open include file: 'libxml/xpath.h': No such file or directory
    ****************************************************************************
*****
    Could not find function xmlCheckVersion in library libxml2. Is libxml2 insta
lled?
    ****************************************************************************
*****
    error: command 'C:\\Users\\xxx\\AppData\\Local\\Programs\\Common\\Micros
oft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

安裝缺失的庫,在cmd輸入easy_install lxml,問題解決
在cmd輸入pyspider, 訪問http://localhost:5000/

3、安裝phantomJs(可選)

去官網(wǎng)下載phantomJs:http://phantomjs.org/download.html
解壓后把bin文件夾添加到系統(tǒng)的環(huán)境變量里,重新運(yùn)行pyspider不會(huì)顯示phantomJs不存在則安裝成功。

PySpider官方教程

二、測試IMDB電影抓取爬蟲

1、創(chuàng)建腳本

打開頁面以后,按create,然后填寫初始的爬取頁面,會(huì)自動(dòng)生成一個(gè)腳本 。

創(chuàng)建腳本

2、編輯腳本

按照官網(wǎng)上的tutorial做了一下,實(shí)驗(yàn)一由于imdb頁面更改的緣故更改了detail_page的應(yīng)用

    @config(priority=2)
    def detail_page(self, response):
        return {
            "url": response.url,
            "title": response.doc('h1').text(),
            "rating": response.doc('span[itemprop="ratingValue"]').text(),
            "director": [x.text() for x in response.doc('[itemprop="director"] span').items()],
            "stars": [x.text() for x in response.doc('[itemprop="actors"] span').items()],
        }

2、測試腳本,按左上角run,然后切換到下面的follow,可是看到每一步爬取的鏈接,點(diǎn)開那個(gè)...可以看詳細(xì)的信息。

測試運(yùn)行腳本

3、運(yùn)行爬蟲

正常來說回到PySpider的dashboard,把任務(wù)狀態(tài)改成running或者debug(沒啥區(qū)別)打鉤,然后點(diǎn)Run就行。右邊那個(gè)active tasks可以看任務(wù)運(yùn)行狀態(tài),results里是爬蟲的結(jié)果。
Tips:如果下載結(jié)果為csv格式,用文本編輯器打開中文顯示正常,而用excel打開是亂碼,記得把csv文件的編碼改成utf16即可

運(yùn)行爬蟲

4、可能遇到的錯(cuò)誤

PySpider坑爹的地方在于刪除以及重新運(yùn)行爬蟲非常麻煩,我不多說了,總之鼓搗了幾下之后遇到了奇怪的錯(cuò)誤:

[W 160223 19:04:58 index:105] connect to scheduler rpc error: error(10061 onnection could be made because the target machine actively refused it')

原因不明,可能是我刪除project不當(dāng)造成的,解決方法不明,有一個(gè)治標(biāo)不治本的方法,新開一個(gè)cmd輸入pyspider scheduler --no-xmlrpc,然后重啟pyspider。這個(gè)方法的缺點(diǎn)是當(dāng)Scheduler重啟以后需要重新輸入這個(gè)命令,否則問題會(huì)反復(fù)出現(xiàn)。當(dāng)出現(xiàn)以下提示說明Scheduler運(yùn)行正常(注意第二行說明載入項(xiàng)目成功):

[I 160225 14:29:57 scheduler:453] loading projects
[I 160225 14:29:57 scheduler:722] select imdb_test:_on_get_info data:,_on_get_in fo
[I 160225 14:29:57 scheduler:394] in 5m: new:0,success:0,retry:0,failed:0

有一個(gè)奇怪的點(diǎn)就是雖然把Python27\Scripts加入了系統(tǒng)環(huán)境變量,但是以上命令有時(shí)候不管用,建議在執(zhí)行pyspiderscheduler配置的時(shí)候都在Scripts目錄下(這樣任務(wù)相關(guān)的data也會(huì)保存在Scripts目錄下)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容