Scrapy1.4最新官方文檔總結(jié) 1 介紹·安裝
Scrapy1.4最新官方文檔總結(jié) 2 Tutorial
Scrapy1.4最新官方文檔總結(jié) 3 命令行工具
這是官方文檔的命令行工具https://docs.scrapy.org/en/latest/topics/commands.html
配置設(shè)置
Scrapy 默認在 scrapy.cfg 文件中查找配置參數(shù):
- 系統(tǒng)范圍:/etc/scrapy.cfg 或 c:\scrapy\scrapy.cfg
- 用戶范圍:~/.config/scrapy.cfg ($XDG_CONFIG_HOME) 和 ~/.scrapy.cfg ($HOME)
- 項目內(nèi)范圍:scrapy.cfg
項目范圍的設(shè)置將覆蓋所有其他文件的設(shè)置,用戶范圍內(nèi)定義的設(shè)置的覆蓋系統(tǒng)范圍內(nèi)的設(shè)置。
Scrapy 也可以接受來自環(huán)境變量的配置。目前有:
- SCRAPY_SETTINGS_MODULE (見 Designating the settings)
- SCRAPY_PROJECT
- SCRAPY_PYTHON_SHELL (見 Scrapy shell)
使用 scrapy 工具
在沒有參數(shù)的情況下直接運行 scrapy 命令將得到一些使用幫助和可用的命令,如下所示:

在之前tutorial下面運行,第一行將打印出當(dāng)前項目的名稱:

創(chuàng)建項目
scrapy startproject myproject [project_dir]
這將在 project_dir 目錄下創(chuàng)建一個 Scrapy 項目。如果沒有指定 project_dir,將會在與 myproject 同名的目錄中創(chuàng)建項目(如果沒有則創(chuàng)建它)。
進入新建項目的根目錄:
cd project_dir
管理項目
新建爬蟲:
scrapy genspider mydomain mydomain.com
Scrapy 的可用命令
查看某個命令的幫助:
scrapy <command> -h
查看所有可用的命令:
scrapy -h
命令分為兩類,全局命令:
startproject
genspider
settings
runspider
shell
fetch
view
version
項目命令:
crawl
check
list
edit
parse
bench
startproject
scrapy startproject myproject
genspider
scrapy genspider [-t template] <name> <domain>
在當(dāng)前文件夾或當(dāng)前項目的 spiders 文件夾中新建一個爬蟲。如果在項目中使用此命令。 <name> 參數(shù)為爬蟲的名稱,<domain> 用于生成 allowed_domains 和 start_urls spider 的屬性。
示例(template:模板):
$ 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'
crawl
語法:scrapy crawl <spider>
必須在項目內(nèi)使用。
啟動爬蟲。
示例:
$ scrapy crawl myspider
[ ... myspider starts crawling ... ]
check
語法:scrapy check [-l] <spider>
必須在項目內(nèi)使用:是
協(xié)議(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
list
語法:scrapy list
必須在項目內(nèi)使用:是
列出項目中所有可用爬蟲。
示例:
$ scrapy list
spider1
spider2
edit
語法:scrapy edit <spider>
必須在項目內(nèi)使用:是
使用EDITOR環(huán)境變量或設(shè)置中定義的編輯器編輯爬蟲。
該命令僅作為一種快捷方式提供,開發(fā)人員可以自由選擇工具或IDE來編寫和調(diào)試爬蟲。
示例:
$ scrapy edit spider1
fetch
語法:scrapy fetch <url>
必須在項目內(nèi)使用:否
使用 Scrapy 下載器下載給定的 URL,并將內(nèi)容輸出到標(biāo)準(zhǔn)輸出流。
這個命令的有趣之處在于它會使用爬蟲定義的方式下載頁面。 例如,如果爬蟲具有 USER_AGENT 屬性覆蓋了 User Agent,那么命令將使用爬蟲里的屬性。
所以這個命令可以用來查看爬蟲如何獲取某個頁面。
在項目之外使用時只會使用默認的 Scrapy 下載器設(shè)置。
支持的選項:
- --spider = SPIDER:強制使用給定的爬蟲
- --headers:打印 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)']}
view
語法:scrapy view <url>
必須在項目內(nèi)使用:否
以 Scrapy 爬蟲所“看到”的樣子在瀏覽器中打開給定的URL。用來查看爬蟲所“看到”的樣子是否是你所期望的,因為兩者有可能不同。
支持的選項:
--spider = SPIDER:強制使用給定的爬蟲
--no-redirect:禁用 HTTP 3xx 重定向(默認啟用)
示例:
$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]
shell
語法:scrapy shell [url]
必須在項目內(nèi)使用:否
以給定的 URL(如果給定)啟動 Scrapy shell。支持 UNIX 風(fēng)格的本地文件路徑,包括相對路徑(./ 或 ../)和絕對路徑。請參閱 Scrapy shell 了解更多信息。
支持的選項:
--spider = SPIDER:強制使用給定的爬蟲
-c code:在 shell 中執(zhí)行代碼,打印結(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/')
# 默認啟用重定向
$ scrapy shell --nolog http://httpbin.org/redirect-to?url=http%3A%2F%2Fexample.com%2F -c '(response.status, response.url)'
(200, 'http://example.com/')
# 你可以通過 --no-redirect 禁用重定向
# (只作用于命令行參數(shù)中的 URL)
$ 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')
parse
語法:scrapy parse <url> [options]
必須在項目內(nèi)使用:是
獲取給定的 URL 并使用爬蟲處理它的方式解析它,使用 --callback 選項傳遞解析方法,默認使用 parse 方法。
支持的選項:
- --spider = SPIDER:強制使用給定的爬蟲
- --a NAME = VALUE:設(shè)置爬蟲參數(shù)(可能會重復(fù))
- --callback 或 -c:解析響應(yīng)對象的回調(diào)方法
- --piplines:通過管道處理項
- --rules 或 -r:使用 CrawlSpider 規(guī)則查找用于解析響應(yīng)對象的回調(diào)方法
- --noitems:不顯示抓取到的項
- --nolinks:不顯示提取的鏈接
- --nocolour:避免使用pygments對輸出著色
- --depth 或 -d:遞歸爬取的深度(默認值:1)
- --verbose 或 -v:顯示爬取每一層的信息
示例:
$ 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 -----------------------------------------------------------------
[]
settings
語法:scrapy settings [options]
必須在項目內(nèi)使用:否
獲取 Scrapy 設(shè)置。
如果在項目中使用它將顯示項目的設(shè)置值,否則將顯示 Scrapy 默認的設(shè)置。
示例:
$ scrapy settings --get BOT_NAME
scrapybot
$ scrapy settings --get DOWNLOAD_DELAY
0
runspider
語法:scrapy runspider <spider_file.py>
必須在項目內(nèi)使用:否
運行一個獨立的爬蟲 Python 文件,無需創(chuàng)建一個項目。
示例:
$ scrapy runspider myspider.py
[ ... spider starts crawling ... ]
version
語法:scrapy version [-v]
必須在項目內(nèi)使用:否
打印 Scrapy 版本。使用 -v 時還會打印出 Python,Twisted 和 Platform 的信息,這對錯誤報告很有用。
bench
語法:scrapy bench
必須在項目內(nèi)使用:否
運行 benchmark 測試。
自定義命令
您還可以使用 COMMANDS_MODULE 設(shè)置添加自定義項目命令。有關(guān)如何實現(xiàn)命令的示例,請參閱 scrapy commands。
COMMANDS_MODULE
默認值:''(空字符串)
用于查找自定義 Scrapy 命令的模塊。用于為您的 Scrapy 項目添加自定義命令。
例:
COMMANDS_MODULE = 'mybot.commands'
通過 setup.py 的 entry points 注冊命令
注意:這是一個實驗性功能,請謹慎使用。
您還可以在 setup.py 文件的 entry point 中添加 scrapy.commands,從外部庫添加 Scrapy 命令。
以下示例添加了 my_command 命令:
from setuptools import setup, find_packages
setup(name='scrapy-mymodule',
entry_points={
'scrapy.commands': [
'my_command=my_scrapy_module.commands:MyCommand',
],
},
)
Scrapy1.4最新官方文檔總結(jié) 1 介紹·安裝
Scrapy1.4最新官方文檔總結(jié) 2 Tutorial
Scrapy1.4最新官方文檔總結(jié) 3 命令行工具