Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Merge branch 'master' into ISSUE-4250-add_batch_deliveries

Adrián Chavesc4f925022054
diff --cc docs/topics/feed-exports.rst
index fdc6e7cba,0dc73c513..2c9774b55
--- a/docs/topics/feed-exports.rst
+++ b/docs/topics/feed-exports.rst
@@@ -427,49 -446,8 +448,52 @@@ format in :setting:`FEED_EXPORTERS`. E.
'csv': None,
}
- .. _URIs: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
- .. _Amazon S3: https://aws.amazon.com/s3/
- .. _botocore: https://github.com/boto/botocore
- .. _Canned ACL: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
+
+.. setting:: FEED_EXPORT_BATCH_ITEM_COUNT
+
+FEED_EXPORT_BATCH_ITEM_COUNT
+-----------------------------
+Default: ``0``
+
+If assigned an integer number higher than ``0``, Scrapy generates multiple output files
+storing up to the specified number of items in each output file.
+
+When generating multiple output files, you must use at least one of the following
+placeholders in the feed URI to indicate how the different output file names are
+generated:
+
+* ``%(batch_time)s`` - gets replaced by a timestamp when the feed is being created
+ (e.g. ``2020-03-28T14-45-08.237134``)
+
+* ``%(batch_id)d`` - gets replaced by the sequence number of the batch.
+
+ Use :ref:`printf-style string formatting <python:old-string-formatting>` to
+ alter the number format. For example, to make the batch ID a 5-digit
+ number by introducing leading zeroes as needed, use ``%(batch_id)05d``
+ (e.g. ``3`` becomes ``00003``, ``123`` becomes ``00123``).
+
+For instance, if your settings include::
+
+ FEED_EXPORT_BATCH_ITEM_COUNT=100
+
+And your :command:`crawl` command line is::
+
+ scrapy crawl spidername -o "dirname/%(batch_id)d-filename%(batch_time)s.json"
+
+The command line above can generate a directory tree like::
+
+->projectname
+-->dirname
+--->1-filename2020-03-28T14-45-08.237134.json
+--->2-filename2020-03-28T14-45-09.148903.json
+--->3-filename2020-03-28T14-45-10.046092.json
+
+Where the first and second files contain exactly 100 items. The last one contains
+100 items or fewer.
++
++
+ .. _URIs: https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
+ .. _Amazon S3: https://aws.amazon.com/s3/
+ .. _botocore: https://github.com/boto/botocore
+ .. _Canned ACL: https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
+ .. _Google Cloud Storage: https://cloud.google.com/storage/
diff --cc scrapy/settings/default_settings.py
index 0016bbe1b,2fd1abbc1..896afa995
--- a/scrapy/settings/default_settings.py
+++ b/scrapy/settings/default_settings.py
@@@ -142,11 -142,11 +142,12 @@@ FEED_STORAGES = {
FEED_STORAGES_BASE = {
'': 'scrapy.extensions.feedexport.FileFeedStorage',
'file': 'scrapy.extensions.feedexport.FileFeedStorage',
- 'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage',
- 's3': 'scrapy.extensions.feedexport.S3FeedStorage',
'ftp': 'scrapy.extensions.feedexport.FTPFeedStorage',
+ 'gs': 'scrapy.extensions.feedexport.GCSFeedStorage',
+ 's3': 'scrapy.extensions.feedexport.S3FeedStorage',
+ 'stdout': 'scrapy.extensions.feedexport.StdoutFeedStorage',
}
+FEED_EXPORT_BATCH_ITEM_COUNT = 0
FEED_EXPORTERS = {}
FEED_EXPORTERS_BASE = {
'json': 'scrapy.exporters.JsonItemExporter',
diff --cc tests/test_feedexport.py
index 4e0b867a4,37384081a..129b7fc0b
--- a/tests/test_feedexport.py
+++ b/tests/test_feedexport.py
@@@ -26,13 -24,24 +26,26 @@@ from zope.interface.verify import verif
import scrapy
from scrapy.crawler import CrawlerRunner
+from scrapy.exceptions import NotConfigured
from scrapy.exporters import CsvItemExporter
- from scrapy.extensions.feedexport import (BlockingFeedStorage, FeedExporter, FileFeedStorage, FTPFeedStorage,
- IFeedStorage, S3FeedStorage, StdoutFeedStorage)
+ from scrapy.extensions.feedexport import (
+ BlockingFeedStorage,
++ FeedExporter,
+ FileFeedStorage,
+ FTPFeedStorage,
+ GCSFeedStorage,
+ IFeedStorage,
+ S3FeedStorage,
+ StdoutFeedStorage,
+ )
from scrapy.settings import Settings
from scrapy.utils.python import to_unicode
- from scrapy.utils.test import assert_aws_environ, get_crawler, get_s3_content_and_delete
+ from scrapy.utils.test import (
+ assert_aws_environ,
+ get_s3_content_and_delete,
+ get_crawler,
+ mock_google_cloud_storage,
+ )
from tests.mockserver import MockServer