W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本節(jié)包含可用的內(nèi)置命令列表,其中包含說明和一些用法示例。記住,您可以通過運行以下命令獲取有關(guān)每個命令的更多信息:
scrapy <command> -h
您可以使用以下命令查看所有可用命令:
scrapy -h
有兩種命令,一種是只從零碎項目(特定于項目的命令)內(nèi)部工作的命令,另一種是不使用活動零碎項目(全局命令)的命令,盡管從項目內(nèi)部運行時它們的行為可能略有不同(因為它們將使用項目覆蓋設(shè)置)。
全局命令:
startproject
?genspider
?settings
?runspider
?shell
?fetch
?view
?version
?僅Project命令:
crawl
?check
?list
?edit
?parse
?bench
?scrapy startproject <project_name> [project_dir]
?創(chuàng)建一個名為 ?project_name
? 下 ?project_dir
? 目錄。如果 ?project_dir
? 沒有指定, ?project_dir
? 將與 ?project_name
? .
使用實例:
$ scrapy startproject myproject
scrapy genspider [-t template] <name> <domain>
?在當(dāng)前文件夾或當(dāng)前項目的 ?spiders
? 文件夾(如果從項目內(nèi)部調(diào)用)。這個 ?<name>
? 參數(shù)設(shè)置為spider的 ?name
? ,同時 ?<domain>
? 用于生成 ?allowed_domains
? 和 ?start_urls
? 蜘蛛的屬性。
使用實例:
$ scrapy genspider -l
Available templates:
basic
crawl
csvfeed
xmlfeed
$ scrapy genspider example example.com
Created spider 'example' using template 'basic'
$ scrapy genspider -t crawl scrapyorg scrapy.org
Created spider 'scrapyorg' using template 'crawl'
這只是一個基于預(yù)先定義的模板創(chuàng)建spider的快捷命令,但肯定不是創(chuàng)建spider的唯一方法。您可以自己創(chuàng)建蜘蛛源代碼文件,而不是使用這個命令。
scrapy crawl <spider>
?開始用蜘蛛爬行。
用法示例:
$ scrapy crawl myspider
[ ... myspider starts crawling ... ]
scrapy check [-l] <spider>
?運行合同檢查。
用法示例:
$ 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)前項目中所有可用的spider。每行輸出一個蜘蛛。
使用實例:
$ scrapy list
spider1
spider2
scrapy edit <spider>
?使用中定義的編輯器編輯給定的蜘蛛 EDITOR 環(huán)境變量或(如果未設(shè)置) ?EDITOR
? 設(shè)置。
這個命令僅作為最常見情況下的快捷方式提供,開發(fā)人員當(dāng)然可以自由選擇任何工具或IDE來編寫和調(diào)試spider。
使用實例:
$ scrapy edit spider1
scrapy fetch <url>
?使用ScrapyDownloader下載給定的URL,并將內(nèi)容寫入標準輸出。
這個命令的有趣之處在于它獲取了蜘蛛如何下載它的頁面。例如,如果蜘蛛 ?USER_AGENT
? 覆蓋用戶代理的屬性,它將使用該屬性。
所以這個命令可以用來“查看”蜘蛛如何獲取特定的頁面。
如果在項目之外使用,則不會應(yīng)用特定的每蜘蛛行為,它只會使用默認的scrapy下載器設(shè)置。
支持的選項:
--spider=SPIDER
? :繞過Spider自動檢測并強制使用特定Spider--headers
? :打印響應(yīng)的HTTP頭而不是響應(yīng)的正文--no-redirect
? :不遵循HTTP 3xx重定向(默認為遵循它們)用法示例:
$ 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,因為您的廢蜘蛛會“看到”它。有時候蜘蛛看到的頁面與普通用戶不同,所以這可以用來檢查蜘蛛“看到”什么,并確認它是你所期望的。
支持的選項:
--spider=SPIDER
? :繞過Spider自動檢測并強制使用特定Spider--no-redirect
:不遵循HTTP 3xx重定向(默認為遵循它們)使用實例:
$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]
scrapy shell [url]
?為給定的URL(如果給定)啟動scrapy shell;如果沒有給定URL,則為空。還支持Unix風(fēng)格的本地文件路徑,無論是相對于 ./ 或 ../ 前綴或絕對文件路徑。見 Scrapy shell 更多信息。
支持的選項:
--spider=SPIDER
? :繞過Spider自動檢測并強制使用特定Spider-c code
? :評估shell中的代碼,打印結(jié)果并退出--no-redirect
? :不遵循HTTP 3xx重定向(默認為遵循它們);這只影響在命令行上作為參數(shù)傳遞的URL;一旦進入shell, ?fetch(url)
? 默認情況下仍將遵循HTTP重定向。使用實例:
$ scrapy shell http://www.example.com/some/page.html
[ ... scrapy shell starts ... ]
$ scrapy shell --nolog http://www.example.com/ -c '(response.status, response.url)'
(200, 'http://www.example.com/')
# shell follows HTTP redirects by default
$ scrapy shell --nolog http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F -c '(response.status, response.url)'
(200, 'http://example.com/')
# you can disable this with --no-redirect
# (only for the URL passed as command line argument)
$ scrapy shell --no-redirect --nolog http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F -c '(response.status, response.url)'
(302, 'http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F')
scrapy parse <url> [options]
?獲取給定的URL,并使用處理它的spider,使用 ?--callback
? 選項,或 ?parse
? 如果沒有給出。
支持的選項:
--spider=SPIDER
? :繞過Spider自動檢測并強制使用特定Spider--a NAME=VALUE
? :set spider參數(shù)(可以重復(fù))--callback
? 或 ?-c
? :用作分析響應(yīng)的回調(diào)的spider方法--meta
? 或 ?-m
? :將傳遞給回調(diào)請求的附加請求元。這必須是有效的JSON字符串。示例:--meta='“foo”:“bar”'--cbkwargs
? :將傳遞給回調(diào)的其他關(guān)鍵字參數(shù)。這必須是有效的JSON字符串。示例:--cbkwargs='“foo”:“bar”'--pipelines
? :通過管道處理項目--rules
? 或 ?-r
? 使用 ?CrawlSpider
? 發(fā)現(xiàn)用于解析響應(yīng)的回調(diào)(即spider方法)的規(guī)則--noitems
? :不顯示爬取的項目--nolinks
? :不顯示提取的鏈接--nocolour
? :避免使用Pygments對輸出著色--depth
? 或 ?-d
? :應(yīng)遞歸執(zhí)行請求的深度級別(默認值:1)--verbose
? 或 ?-v
? :顯示每個深度級別的信息--output
? 或 ?-o
? :將刮取的項目轉(zhuǎn)儲到文件2.3 新版功能.使用實例:
$ scrapy parse http://www.example.com/ -c parse_item
[ ... scrapy log lines crawling example.com spider ... ]
>>> STATUS DEPTH LEVEL 1 <<<
# Scraped Items ------------------------------------------------------------
[{'name': 'Example item',
'category': 'Furniture',
'length': '12 cm'}]
# Requests -----------------------------------------------------------------
[]
scrapy settings [options]
?獲取 Scrapy 設(shè)置的值。
如果在項目中使用,它將顯示項目設(shè)置值,否則它將顯示該設(shè)置的默認 Scrapy 值。
示例用法:
$ scrapy settings --get BOT_NAME
scrapybot
$ scrapy settings --get DOWNLOAD_DELAY
0
scrapy runspider <spider_file.py>
?運行一個包含在python文件中的spider,而不必創(chuàng)建一個項目。
示例用法:
$ scrapy runspider myspider.py
[ ... spider starts crawling ... ]
Syntax: scrapy version [-v]
?打印殘缺版本。如果使用 ?-v
? 它還打印python、twisted和platform信息,這對bug報告很有用。
scrapy bench
?運行一個快速基準測試。 標桿管理 .
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: