Feed exports

New in version 0.10.

One of the most frequently required features when implementing scrapers is being able to store the scraped data properly and, quite often, that means generating an “export file” with the scraped data (commonly called “export feed”) to be consumed by other systems.

Scrapy provides this functionality out of the box with the Feed Exports, which allows you to generate feeds with the scraped items, using multiple serialization formats and storage backends.

Serialization formats

For serializing the scraped data, the feed exports use the Item exporters. These formats are supported out of the box:

But you can also extend the supported format through the FEED_EXPORTERS setting.

JSON

JSON lines

CSV

  • Value for the format key in the FEEDS setting: csv

  • Exporter used: CsvItemExporter

  • To specify columns to export and their order use FEED_EXPORT_FIELDS. Other feed exporters can also use this option, but it is important for CSV because unlike many other export formats CSV uses a fixed header.

XML

Pickle

Marshal

Storages

When using the feed exports you define where to store the feed using one or multiple URIs (through the FEEDS setting). The feed exports supports multiple storage backend types which are defined by the URI scheme.

The storages backends supported out of the box are:

Some storage backends may be unavailable if the required external libraries are not available. For example, the S3 backend is only available if the botocore library is installed.

Storage URI parameters

The storage URI can also contain parameters that get replaced when the feed is being created. These parameters are:

  • %(time)s - gets replaced by a timestamp when the feed is being created

  • %(name)s - gets replaced by the spider name

Any other named parameter gets replaced by the spider attribute of the same name. For example, %(site_id)s would get replaced by the spider.site_id attribute the moment the feed is being created.

Here are some examples to illustrate:

  • Store in FTP using one directory per spider:

    • ftp://user:password@ftp.example.com/scraping/feeds/%(name)s/%(time)s.json
  • Store in S3 using one directory per spider:

    • s3://mybucket/scraping/feeds/%(name)s/%(time)s.json

Storage backends

Local filesystem

The feeds are stored in the local filesystem.

  • URI scheme: file

  • Example URI: file:///tmp/export.csv

  • Required external libraries: none

Note that for the local filesystem storage (only) you can omit the scheme if you specify an absolute path like /tmp/export.csv. This only works on Unix systems though.

FTP

The feeds are stored in a FTP server.

  • URI scheme: ftp

  • Example URI: ftp://user:pass@ftp.example.com/path/to/export.csv

  • Required external libraries: none

FTP supports two different connection modes: active or passive. Scrapy uses the passive connection mode by default. To use the active connection mode instead, set the FEED_STORAGE_FTP_ACTIVE setting to True.

S3

The feeds are stored on Amazon S3.

  • URI scheme: s3

  • Example URIs:

    • s3://mybucket/path/to/export.csv

    • s3://aws_key:aws_secret@mybucket/path/to/export.csv

  • Required external libraries: botocore

The AWS credentials can be passed as user/password in the URI, or they can be passed through the following settings:

You can also define a custom ACL for exported feeds using this setting:

Standard output

The feeds are written to the standard output of the Scrapy process.

  • URI scheme: stdout

  • Example URI: stdout:

  • Required external libraries: none

Settings

These are the settings used for configuring the feed exports:

FEEDS

New in version 2.1.

Default: {}

A dictionary in which every key is a feed URI (or a pathlib.Path object) and each value is a nested dictionary containing configuration parameters for the specific feed. This setting is required for enabling the feed export feature.

See Storage backends for supported URI schemes.

For instance:

  1. {
  2. 'items.json': {
  3. 'format': 'json',
  4. 'encoding': 'utf8',
  5. 'store_empty': False,
  6. 'fields': None,
  7. 'indent': 4,
  8. },
  9. '/home/user/documents/items.xml': {
  10. 'format': 'xml',
  11. 'fields': ['name', 'price'],
  12. 'encoding': 'latin1',
  13. 'indent': 8,
  14. },
  15. pathlib.Path('items.csv'): {
  16. 'format': 'csv',
  17. 'fields': ['price', 'name'],
  18. },
  19. }

The following is a list of the accepted keys and the setting that is used as a fallback value if that key is not provided for a specific feed definition.

FEED_EXPORT_ENCODING

Default: None

The encoding to be used for the feed.

If unset or set to None (default) it uses UTF-8 for everything except JSON output, which uses safe numeric encoding (\uXXXX sequences) for historic reasons.

Use utf-8 if you want UTF-8 for JSON too.

FEED_EXPORT_FIELDS

Default: None

A list of fields to export, optional. Example: FEED_EXPORT_FIELDS = ["foo", "bar", "baz"].

Use FEED_EXPORT_FIELDS option to define fields to export and their order.

When FEED_EXPORT_FIELDS is empty or None (default), Scrapy uses fields defined in dicts or Item subclasses a spider is yielding.

If an exporter requires a fixed set of fields (this is the case for CSV export format) and FEED_EXPORT_FIELDS is empty or None, then Scrapy tries to infer field names from the exported data - currently it uses field names from the first item.

FEED_EXPORT_INDENT

Default: 0

Amount of spaces used to indent the output on each level. If FEED_EXPORT_INDENT is a non-negative integer, then array elements and object members will be pretty-printed with that indent level. An indent level of 0 (the default), or negative, will put each item on a new line. None selects the most compact representation.

Currently implemented only by JsonItemExporter and XmlItemExporter, i.e. when you are exporting to .json or .xml.

FEED_STORE_EMPTY

Default: False

Whether to export empty feeds (i.e. feeds with no items).

FEED_STORAGES

Default: {}

A dict containing additional feed storage backends supported by your project. The keys are URI schemes and the values are paths to storage classes.

FEED_STORAGE_FTP_ACTIVE

Default: False

Whether to use the active connection mode when exporting feeds to an FTP server (True) or use the passive connection mode instead (False, default).

For information about FTP connection modes, see What is the difference between active and passive FTP?.

FEED_STORAGE_S3_ACL

Default: '' (empty string)

A string containing a custom ACL for feeds exported to Amazon S3 by your project.

For a complete list of available values, access the Canned ACL section on Amazon S3 docs.

FEED_STORAGES_BASE

Default:

  1. {
  2. '': 'scrapy.extensions.feedexport.FileFeedStorage',
  3. 'file': 'scrapy.extensions.feedexport.FileFeedStorage',
  4. 'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage',
  5. 's3': 'scrapy.extensions.feedexport.S3FeedStorage',
  6. 'ftp': 'scrapy.extensions.feedexport.FTPFeedStorage',
  7. }

A dict containing the built-in feed storage backends supported by Scrapy. You can disable any of these backends by assigning None to their URI scheme in FEED_STORAGES. E.g., to disable the built-in FTP storage backend (without replacement), place this in your settings.py:

  1. FEED_STORAGES = {
  2. 'ftp': None,
  3. }

FEED_EXPORTERS

Default: {}

A dict containing additional exporters supported by your project. The keys are serialization formats and the values are paths to Item exporter classes.

FEED_EXPORTERS_BASE

Default:

  1. {
  2. 'json': 'scrapy.exporters.JsonItemExporter',
  3. 'jsonlines': 'scrapy.exporters.JsonLinesItemExporter',
  4. 'jl': 'scrapy.exporters.JsonLinesItemExporter',
  5. 'csv': 'scrapy.exporters.CsvItemExporter',
  6. 'xml': 'scrapy.exporters.XmlItemExporter',
  7. 'marshal': 'scrapy.exporters.MarshalItemExporter',
  8. 'pickle': 'scrapy.exporters.PickleItemExporter',
  9. }

A dict containing the built-in feed exporters supported by Scrapy. You can disable any of these exporters by assigning None to their serialization format in FEED_EXPORTERS. E.g., to disable the built-in CSV exporter (without replacement), place this in your settings.py:

  1. FEED_EXPORTERS = {
  2. 'csv': None,
  3. }