新版功能。
Scrapy 是通過 scrapy 命令行工具進(jìn)行控制的。 這里我們稱之為 “Scrapy tool” 以用來和子命令進(jìn)行區(qū)分。對于子命令,我們稱為 “command” 或者 “Scrapy commands”。
Scrapy tool 針對不同的目的提供了多個命令,每個命令支持不同的參數(shù)和選項(xiàng)。
在開始對命令行工具以及子命令的探索前,讓我們首先了解一下 Scrapy 的項(xiàng)目的目錄結(jié)構(gòu)。
雖然可以被修改,但所有的 Scrapy 項(xiàng)目默認(rèn)有類似于下邊的文件結(jié)構(gòu):
scrapy.cfg
myproject/
__init__.py
items.py
pipelines.py
settings.py
spiders/
__init__.py
spider1.py
spider2.py
...
scrapy.cfg
存放的目錄被認(rèn)為是 項(xiàng)目的根目錄 。該文件中包含 python 模塊名的字段定義了項(xiàng)目的設(shè)置。例如:
[settings]
default = myproject.settings
您可以以無參數(shù)的方式啟動 Scrapy 工具。該命令將會給出一些使用幫助以及可用的命令:
Scrapy X.Y - no active project
Usage:
scrapy <command> [options] [args]
Available commands:
crawl Run a spider
fetch Fetch a URL using the Scrapy downloader
[...]
如果您在 Scrapy 項(xiàng)目中運(yùn)行,當(dāng)前激活的項(xiàng)目將會顯示在輸出的第一行。上面的輸出就是響應(yīng)的例子。如果您在一個項(xiàng)目中運(yùn)行命令將會得到類似的輸出:
Scrapy X.Y - project: myproject
Usage:
scrapy <command> [options] [args]
[...]
一般來說,使用 scrapy
工具的第一件事就是創(chuàng)建您的 Scrapy 項(xiàng)目:
scrapy startproject myproject
該命令將會在 myproject 目錄中創(chuàng)建一個 Scrapy 項(xiàng)目。
接下來,進(jìn)入到項(xiàng)目目錄中:
cd myproject
這時候您就可以使用 scrapy 命令來管理和控制您的項(xiàng)目了。
您可以在您的項(xiàng)目中使用 scrapy
工具來對其進(jìn)行控制和管理。
比如,創(chuàng)建一個新的 spider:
scrapy genspider mydomain mydomain.com
有些 Scrapy 命令(比如 crawl
)要求必須在 Scrapy 項(xiàng)目中運(yùn)行。 您可以通過下邊的 commands reference
來了解哪些命令需要在項(xiàng)目中運(yùn)行,哪些不用。
另外要注意,有些命令在項(xiàng)目里運(yùn)行時的效果有些許區(qū)別。 以 fetch 命令為例,如果被爬取的 url 與某個特定 spider 相關(guān)聯(lián), 則該命令將會使用 spider 的動作(spider-overridden behaviours)。 (比如 spider 指定的 user_agent
)。 該表現(xiàn)是有意而為之的。一般來說, fetch
命令就是用來測試檢查 spider 是如何下載頁面。
該章節(jié)提供了可用的內(nèi)置命令的列表。每個命令都提供了描述以及一些使用例子。您總是可以通過運(yùn)行命令來獲取關(guān)于每個命令的詳細(xì)內(nèi)容:
scrapy <command> -h
您也可以查看所有可用的命令:
scrapy -h
Scrapy 提供了兩種類型的命令。一種必須在 Scrapy 項(xiàng)目中運(yùn)行(針對項(xiàng)目(Project-specific)的命令),另外一種則不需要(全局命令)。全局命令在項(xiàng)目中運(yùn)行時的表現(xiàn)可能會與在非項(xiàng)目中運(yùn)行有些許差別(因?yàn)榭赡軙褂庙?xiàng)目的設(shè)定)。
全局命令:
startproject
settings
runspider
shell
fetch
view
version
項(xiàng)目(Project-only)命令:
crawl
check
list
edit
parse
genspider
deploy
bench
scrapy startproject <project_name>
在 project_name 文件夾下創(chuàng)建一個名為 project_name 的 Scrapy 項(xiàng)目。
例子:
$ scrapy startproject myproject
scrapy genspider [-t template] <name> <domain>
在當(dāng)前項(xiàng)目中創(chuàng)建 spider。
這僅僅是創(chuàng)建 spider 的一種快捷方法。該方法可以使用提前定義好的模板來生成 spider。您也可以自己創(chuàng)建 spider 的源碼文件。
例子:
$ scrapy genspider -l
Available templates:
basic
crawl
csvfeed
xmlfeed
$ scrapy genspider -d basic
import scrapy
class $classname(scrapy.Spider):
name = "$name"
allowed_domains = ["$domain"]
start_urls = (
'http://www.$domain/',
)
def parse(self, response):
pass
$ scrapy genspider -t basic example example.com
Created spider 'example' using template 'basic' in module:
mybot.spiders.example
scrapy crawl <spider>
使用 spider 進(jìn)行爬取。
例子:
$ scrapy crawl myspider
[ ... myspider starts crawling ... ]
scrapy check [-l] <spider>
運(yùn)行 contract 檢查。
例子:
$ scrapy check -l
first_spider
* parse
* parse_item
second_spider
* parse
* parse_item
$ scrapy check
[FAILED] first_spider:parse_item
>>> 'RetailPricex' field is missing
[FAILED] first_spider:parse
>>> Returned 92 requests, expected 0..4
scrapy list
列出當(dāng)前項(xiàng)目中所有可用的 spider。每行輸出一個 spider。
使用例子:
$ scrapy list
spider1
spider2
scrapy edit <spider>
使用 EDITOR 中設(shè)定的編輯器編輯給定的 spider
該命令僅僅是提供一個快捷方式。開發(fā)者可以自由選擇其他工具或者 IDE 來編寫調(diào)試 spider。
例子:
$ scrapy edit spider1
scrapy fetch <url>
使用 Scrapy 下載器(downloader)下載給定的 URL,并將獲取到的內(nèi)容送到標(biāo)準(zhǔn)輸出。
該命令以 spider 下載頁面的方式獲取頁面。例如,如果 spider 有 USER_AGENT
屬性修改了 User Agent,該命令將會使用該屬性。
因此,您可以使用該命令來查看 spider 如何獲取某個特定頁面。
該命令如果非項(xiàng)目中運(yùn)行則會使用默認(rèn) Scrapy downloader 設(shè)定。
例子:
$ scrapy fetch --nolog http://www.example.com/some/page.html
[ ... html content here ... ]
$ scrapy fetch --nolog --headers http://www.example.com/
{'Accept-Ranges': ['bytes'],
'Age': ['1263 '],
'Connection': ['close '],
'Content-Length': ['596'],
'Content-Type': ['text/html; charset=UTF-8'],
'Date': ['Wed, 18 Aug 2010 23:59:46 GMT'],
'Etag': ['"573c1-254-48c9c87349680"'],
'Last-Modified': ['Fri, 30 Jul 2010 15:30:18 GMT'],
'Server': ['Apache/2.2.3 (CentOS)']}
scrapy view <url>
在瀏覽器中打開給定的 URL,并以 Scrapy spider 獲取到的形式展現(xiàn)。 有些時候 spider 獲取到的頁面和普通用戶看到的并不相同。 因此該命令可以用來檢查 spider 所獲取到的頁面,并確認(rèn)這是您所期望的。
例子:
$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]
scrapy shell [url]
以給定的 URL(如果給出)或者空(沒有給出 URL)啟動 Scrapy shell。查看 Scrapy 終端(Scrapy shell) 獲取更多信息。
例子:
$ scrapy shell http://www.example.com/some/page.html
[ ... scrapy shell starts ... ]
scrapy parse <url> [options]
獲取給定的 URL 并使用相應(yīng)的 spider 分析處理。如果您提供--callback
選項(xiàng),則使用 spider 的該方法處理,否則使用 parse
。
支持的選項(xiàng):
--spider=SPIDER
:跳過自動檢測 spider 并強(qiáng)制使用特定的 spider--a NAME=VALUE
:設(shè)置 spider 的參數(shù)(可能被重復(fù))--callback or -c
:spider 中用于解析返回(response)的回調(diào)函數(shù)--pipelines
:在 pipeline 中處理 item--rules or -r
:使用 CrawlSpider 規(guī)則來發(fā)現(xiàn)用來解析返回(response)的回調(diào)函數(shù)--noitems
:不顯示爬取到的 item--nolinks
:不顯示提取到的鏈接--nocolour
:避免使用 pygments 對輸出著色--depth or -d
:指定跟進(jìn)鏈接請求的層次數(shù)(默認(rèn):1)--verbose or -v
:顯示每個請求的詳細(xì)信息例子:
$ scrapy parse http://www.example.com/ -c parse_item
[ ... scrapy log lines crawling example.com spider ... ]
>>> STATUS DEPTH LEVEL 1 <<<
# Scraped Items ------------------------------------------------------------
[{'name': u'Example item',
'category': u'Furniture',
'length': u'12 cm'}]
# Requests -----------------------------------------------------------------
[]
scrapy settings [options]
獲取 Scrapy 的設(shè)定
在項(xiàng)目中運(yùn)行時,該命令將會輸出項(xiàng)目的設(shè)定值,否則輸出 Scrapy 默認(rèn)設(shè)定。
例子:
$ scrapy settings --get BOT_NAME
scrapybot
$ scrapy settings --get DOWNLOAD_DELAY
0
scrapy runspider <spider_file.py>
在未創(chuàng)建項(xiàng)目的情況下,運(yùn)行一個編寫在 Python 文件中的 spider。
例子:
$ scrapy runspider myspider.py
[ ... spider starts crawling ... ]
scrapy version [-v]
輸出 Scrapy 版本。配合 -v 運(yùn)行時,該命令同時輸出 Python,Twisted 以及平臺的信息,方便 bug 提交。
新版功能。
scrapy deploy [ <target:project> | -l <target> | -L ]
將項(xiàng)目部署到 Scrapyd 服務(wù)。查看部署您的項(xiàng)目。
新版功能。
運(yùn)行 benchmark 測試。Benchmarking。
您也可以通過 COMMANDS_MODULE 來添加您自己的項(xiàng)目命令。您可以以 scrapy/commands 中 Scrapy commands 為例來了解如何實(shí)現(xiàn)您的命令。
Default: '' (empty string)
用于查找添加自定義 Scrapy 命令的模塊。
例子:
COMMANDS_MODULE = 'mybot.commands'