Scrapy 1.4 文檔 05 命令行工具

在系統命令行中,使用 scrapy 命令能夠建立工程或啓動爬蟲,它控制着 Scrapy 的行爲,咱們稱之爲 Scrapy 命令行工具(command-line tool)或 Scrapy 工具(Scrapy tool)。緊跟在 scrapy 命令以後的命令屬於子命令(咱們稱之爲「命令(commands)」或「Scrapy命令(Scrapy commands)」,例如用於新建項目的 startproject 命令)。html

Scrapy 工具包含許多命令,有各自的功能、參數和選項。python

(scrapy deploy 命令已在1.0中刪除,取而代之的是 scryd-deploy,請參閱 Deploying your project。)git

配置設置

Scrapy 默認在 scrapy.cfg 文件中查找配置參數:github

  1. 系統範圍:/etc/scrapy.cfg 或 c:\scrapy\scrapy.cfg
  2. 用戶範圍:~/.config/scrapy.cfg ($XDG_CONFIG_HOME)  和  ~/.scrapy.cfg ($HOME)
  3. 項目內範圍:scrapy.cfg

項目範圍的設置將覆蓋全部其餘文件的設置,用戶範圍內定義的設置的覆蓋系統範圍內的設置。shell

Scrapy 也能夠接受來自環境變量的配置。目前有:瀏覽器

Scrapy項目的默認結構

在深刻了解命令行工具及其子命令以前,先來看一下 Scrapy 項目的目錄結構。dom

雖然能夠修改,但默認全部的 Scrapy 項目都具備相同的文件結構,與此相似:scrapy

scrapy.cfg
myproject/
    __init__.py
    items.py
    pipelines.py
    settings.py
    spiders/
        __init__.py
        spider1.py
        spider2.py
        ...

scrapy.cfg 文件所在的目錄稱爲項目根目錄(project root directory)。 該文件中聲明瞭項目設置所在的 python 模塊。例如:編輯器

[settings]
default = myproject.settings

(myproject.settings 指 /myproject/settings.py)ide

使用 scrapy 工具

在沒有參數的狀況下直接運行 scrapy 命令將獲得一些使用幫助和可用的命令,以下所示(X.Y 是版本號):

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 項目的根目錄中,第一行將打印出當前項目的名稱。 在上面的例子中,它是從項目外部運行的。若是從項目裏面運行,它會打印出這樣的東西:

Scrapy X.Y - project: myproject

Usage:
  scrapy <command> [options] [args]

[...]

建立項目

一般您使用 scrapy 工具的第一件事是建立您的 Scrapy 項目:

scrapy startproject myproject [project_dir]

這將在 project_dir 目錄下建立一個 Scrapy 項目。若是沒有指定 project_dir,將會在與 myproject 同名的目錄中建立項目(若是沒有則建立它)。

接下來,進入新建項目的根目錄:

cd project_dir

如今,您已經準備好了使用 scrapy 命令來管理和控制您的項目。

管理項目

 在項目內(即在項目根目錄下)您可使用 scrapy 工具來控制和管理您的項目。

例如,新建一個爬蟲:

scrapy genspider mydomain mydomain.com

一些 Scrapy 命令(如 crawl)必須在 Scrapy 項目內運行。 具體請參閱下一節:scrapy 工具中的命令。

一些命令在從項目內部運行時可能稍有不一樣。 例如,若是要獲取的 URL 與某個特定的爬蟲相關聯,則 fetch 命令將使用爬蟲重載的行爲(例如爬蟲的
user_agent 屬性將覆蓋默認的 user-agent)。 這是有意的,由於 fetch 命令旨在檢查爬蟲是怎樣下載頁面的。

scrapy 工具中的命令

本節介紹 Scrapy 的內置命令、說明和一些示例。 記住,您能夠隨時經過運行下面的命令來獲取有關每一個命令的更多信息:

scrapy <command> -h

您也經過下面的命令查看全部可用的命令:

scrapy -h

有兩種命令,僅在 Scrapy 項目內工做的命令(項目命令:Project-specific commands)以及也能夠在項目外工做的命令(全局命令:Global commands),儘管它們在項目內運行時可能會略有不一樣(此時會使用項目重載的設置)。

全局命令:

  • startproject
  • genspider
  • settings
  • runspider
  • shell
  • fetch
  • view
  • version

項目命令:

 

  • crawl
  • check
  • list
  • edit
  • parse
  • bench

 

startproject

  • 語法:scrapy startproject <project_name> [project_dir]
  • 必須在項目內使用:否

project_dir 目錄下新建一個名爲 project_name 的 Scrapy 項目。 若是沒有指定 project_dirproject_dir 將與 myproject 同名。

示例:

$ scrapy startproject myproject

genspider

  • 語法:scrapy genspider [-t template] <name> <domain>
  • 必須在項目內使用:否

在當前文件夾或當前項目的 spiders 文件夾中新建一個爬蟲。若是在項目中使用此命令。 <name> 參數爲爬蟲的名稱,<domain> 用於生成 allowed_domainsstart_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>
  • 必須在項目內使用:是

啓動爬蟲。

示例:

$ scrapy crawl myspider
[ ... myspider starts crawling ... ]

check

  • 語法:scrapy check [-l] <spider>
  • 必須在項目內使用:是

協議(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
  • 必須在項目內使用:是

列出項目中全部可用爬蟲。

示例:

$ scrapy list
spider1
spider2

edit

  • 語法:scrapy edit <spider>
  • 必須在項目內使用:是

使用EDITOR環境變量或設置中定義的編輯器編輯爬蟲。

該命令僅做爲一種快捷方式提供,開發人員能夠自由選擇工具或IDE來編寫和調試爬蟲。

示例:

$ scrapy edit spider1

fetch

  • 語法:scrapy fetch <url>
  • 必須在項目內使用:否

使用 Scrapy 下載器下載給定的 URL,並將內容輸出到標準輸出流。

這個命令的有趣之處在於它會使用爬蟲定義的方式下載頁面。 例如,若是爬蟲具備 USER_AGENT 屬性覆蓋了 User Agent,那麼命令將使用爬蟲裏的屬性。

因此這個命令能夠用來查看爬蟲如何獲取某個頁面。

在項目以外使用時只會使用默認的 Scrapy 下載器設置。

支持的選項:

  • --spider = SPIDER:強制使用給定的爬蟲
  • --headers:打印 HTTP 響應頭
  • --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>
  • 必須在項目內使用:否

以 Scrapy 爬蟲所「看到」的樣子在瀏覽器中打開給定的URL。用來查看爬蟲所「看到」的樣子是不是你所指望的,由於二者有可能不一樣。

支持的選項:

  • --spider = SPIDER:強制使用給定的爬蟲
  • --no-redirect:禁用 HTTP 3xx 重定向(默認啓用)

示例:

$ scrapy view http://www.example.com/some/page.html
[ ... browser starts ... ]

shell

  • 語法:scrapy shell [url]
  • 必須在項目內使用:否

以給定的 URL(若是給定)啓動 Scrapy shell。支持 UNIX 風格的本地文件路徑,包括相對路徑(./../)和絕對路徑。請參閱 Scrapy shell 瞭解更多信息。

支持的選項:

  • --spider = SPIDER:強制使用給定的爬蟲
  • -c code:在 shell 中執行代碼,打印結果並退出
  • --no-redirect:禁用 HTTP 3xx 重定向(默認啓用); 這隻會影響您在命令行參數中給定的 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 禁用重定向
# (只做用於命令行參數中的 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]
  • 必須在項目內使用:是

 獲取給定的 URL 並使用爬蟲處理它的方式解析它,使用 --callback 選項傳遞解析方法,默認使用 parse 方法。

 支持的選項:

  • --spider = SPIDER:強制使用給定的爬蟲
  • --a NAME = VALUE:設置爬蟲參數(可能會重複)
  • --callback-c:解析響應對象的回調方法
  • --piplines:經過管道處理項
  • --rules-r:使用 CrawlSpider 規則查找用於解析響應對象的回調方法
  • --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]
  • 必須在項目內使用:否

獲取 Scrapy 設置。

若是在項目中使用它將顯示項目的設置值,不然將顯示 Scrapy 默認的設置。

示例:

$ scrapy settings --get BOT_NAME
scrapybot
$ scrapy settings --get DOWNLOAD_DELAY
0

runspider

  • 語法:scrapy runspider <spider_file.py>
  • 必須在項目內使用:否

運行一個獨立的爬蟲 Python 文件,無需建立一個項目。

示例:

$ scrapy runspider myspider.py
[ ... spider starts crawling ... ]

version

  • 語法:scrapy version [-v]
  • 必須在項目內使用:否

打印 Scrapy 版本。使用 -v 時還會打印出 Python,Twisted 和 Platform 的信息,這對錯誤報告頗有用。

bench

  • 語法:scrapy bench
  • 必須在項目內使用:否

運行 benchmark 測試。

 自定義命令

您還可使用 COMMANDS_MODULE 設置添加自定義項目命令。有關如何實現命令的示例,請參閱 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',
    ],
  },
 )
相關文章
相關標籤/搜索