-t 开关在 scrapy 中做什么?

What -t switch do in scrapy?

在 scrapy 教程中说,要将输出保存为 csv 或任何其他格式,我们应该使用此命令:

scrapy crawl spider -o result.csv -t csv

一般我们可以使用这个命令:

scrapy crawl my_spider -o file_name.extension -t extension

但是我在没有-t的情况下使用了这个命令,没有问题:

scrapy crawl spider -o result.csv

我的问题是 -t 的作用是什么?

当您不确定时,look into the source code

根据crawl.py source code,如果你没有明确指定格式,Scrapy会检测到它——文件名的扩展名将被用作格式:

if not opts.output_format:
    opts.output_format = os.path.splitext(opts.output)[1].replace(".", "")

在您的情况下,将使用 csv

您通常可以通过调用带有 --help 选项的命令来获得命令行工具选项的解释:

C:\>scrapy crawl --help

Usage
=====
  scrapy crawl [options] <spider>

Run a spider

Options
=======
--help, -h              show this help message and exit
-a NAME=VALUE           set spider argument (may be repeated)
--output=FILE, -o FILE  dump scraped items into FILE (use - for stdout)
--output-format=FORMAT, -t FORMAT
                        format to use for dumping items with -o
...

所以-t用于指定将项目转储到文件时使用的格式。