鍍金池/ 教程/ Java/ Scrapy Shell
Scrapy項目加載器(Item Loader)
Scrapy快速入門
Scrapy蜘蛛(Spider)
Scrapy教程
Scrapy定義項目
Scrapy執(zhí)行爬行捉取
Scrapy項目(Items)
Scrapy安裝
Scrapy提取項目
Scrapy第一個Spider
Scrapy選擇器(Selector)
Scrapy Shell
Scrapy創(chuàng)建項目
Scrapy命令行工具
Scrapy日志
Scrapy使用項目

Scrapy Shell

Scrapy shell 可用于抓取數(shù)據(jù)并提示錯誤代碼,而無需使用蜘蛛。 Scrapy shell的主要目的是測試所提取的代碼,XPath或CSS表達(dá)式。它還用來從中指定刮取數(shù)據(jù)的網(wǎng)頁。

配置Shell

shell 可以通過安裝 IPython(用于交互式計算)控制臺,它是強(qiáng)大的交互式的Shell,提供自動完成,彩色輸出等功能。

如果您在UNIX平臺上工作,那么最好安裝 IPython。 如果有IPython的無法訪問,您也可以使用bpython。

您可以通過設(shè)置 SCRAPY_PYTHON_SHELL 環(huán)境變量或者在 scrapy.cfg 文件中定義配置 Shell,如下圖所示:
[settings]
shell = bpython

啟動Shell

Scrapy shell 可以用下面的命令來啟動:
scrapy shell <url>
url 是指定為需要進(jìn)行數(shù)據(jù)抓取的URL

使用Shell

shell提供一些附加快捷方式和Scrapy對象,如下所述:

可用快捷方式

shell提供可在項目中使用的快捷方式如下:
S.N
快捷方式和說明
1 shelp()
它提供了可用對象和快捷方式的幫助選項
2 fetch(request_or_url)
它會從請求或URL的響應(yīng)收集相關(guān)對象可能的更新
3 view(response)
可以在本地瀏覽器查看特定請求的響應(yīng),觀察和正確顯示外部鏈接,追加基本標(biāo)簽到響應(yīng)正文。

可用Scrapy對象

shell在項目中提供以下可用Scrapy對象:
S.N.
對象和說明
1 crawler
它指定當(dāng)前爬行對象
2 spider
如果對于當(dāng)前網(wǎng)址沒有蜘蛛,那么它將通過定義新的蜘蛛處理URL或蜘蛛對象
3 request
它指定了最后采集頁面請求對象
4 response
它指定了最后采集頁面響應(yīng)對象
5 settings
它提供當(dāng)前Scrapy設(shè)置

Shell會話示例

讓我們試著刮取 scrapy.org 網(wǎng)站,然后開始從 yiibai.com 抓取數(shù)據(jù),如下所述:
在繼續(xù)之前,我們將首先啟動shell,執(zhí)行如下面的命令:
scrapy shell 'http://scrapy.org' --nolog
當(dāng)使用上面的URL,Scrapy將顯示可用的對象:
[s] Available Scrapy objects:
[s]   crawler    
[s]   item       {}
[s]   request    
[s]   response   <200 http://scrapy.org>
[s]   settings   
[s]   spider     
[s] Useful shortcuts:
[s]   shelp()           Provides available objects and shortcuts with help option
[s]   fetch(req_or_url) Collects the response from the request or URL and associated objects will get update
[s]   view(response)    View the response for the given request
接著,對象的工作開始,如下所示:
>> response.xpath('//title/text()').extract_first()
u'Scrapy | A Fast and Powerful Scraping and Web Crawling Framework'

>> fetch("http://reddit.com")
[s] Available Scrapy objects:
[s]   crawler    
[s]   item       {}
[s]   request    
[s]   response   <200 https://www.yiibai.com/>
[s]   settings   
[s]   spider     
[s] Useful shortcuts:
[s]   shelp()           Shell help (print this help)
[s]   fetch(req_or_url) Fetch request (or URL) and update local objects
[s]   view(response)    View response in a browser

>> response.xpath('//title/text()').extract()
[u'reddit: the front page of the internet']

>> request = request.replace(method="POST")

>> fetch(request)
[s] Available Scrapy objects:
[s]   crawler    
...

從Spider檢查響應(yīng)調(diào)用Shell

您可以檢查它是由蜘蛛處理的響應(yīng),只有期望得到的響應(yīng)。
例如:
import scrapy
class SpiderDemo(scrapy.Spider):
    name = "spiderdemo"
    start_urls = [
        "http://yiibai.com",
        "http://yiibai.org",
        "http://yiibai.net",
    ]

    def parse(self, response):
        # You can inspect one specific response
        if ".net" in response.url:
            from scrapy.shell import inspect_response
            inspect_response(response, self)
正如上面的代碼所示,可以從蜘蛛調(diào)用shell,通過使用下面的函數(shù)來檢查響應(yīng):
scrapy.shell.inspect_response
現(xiàn)在運行的蜘蛛,應(yīng)該會得到如下界面:
2016-02-08 18:15:20-0400 [scrapy] DEBUG: Crawled (200)  (referer: None)
2016-02-08 18:15:20-0400 [scrapy] DEBUG: Crawled (200)  (referer: None)
2016-02-08 18:15:20-0400 [scrapy] DEBUG: Crawled (200)  (referer: None)
[s] Available Scrapy objects:
[s]   crawler    
...

>> response.url
'http://yiibai.org'
您可以使用下面的代碼檢查提取的代碼是否正常工作:
>> response.xpath('//div[@class="val"]')
It displays the output as
[]
上面一行只顯示空白輸出。現(xiàn)在可以調(diào)用 shell 來檢查響應(yīng),如下圖所示:
>> view(response)
It displays the response as
True