Components¶
A Scrapy component is any class whose objects are created using
scrapy.utils.misc.create_instance()
.
That includes the classes that you may assign to the following settings:
Third-party Scrapy components may also let you define additional Scrapy components, usually configurable through settings, to modify their behavior.
Enforcing component requirements¶
Sometimes, your components may only be intended to work under certain conditions. For example, they may require a minimum version of Scrapy to work as intended, or they may require certain settings to have specific values.
In addition to describing those conditions in the documentation of your
component, it is a good practice to raise an exception from the __init__
method of your component if those conditions are not met at run time.
In the case of downloader middlewares,
extensions, item pipelines, and spider middlewares, you should raise
scrapy.exceptions.NotConfigured
, passing a description of the issue as a
parameter to the exception so that it is printed in the logs, for the user to
see. For other components, feel free to raise whatever other exception feels
right to you; for example, RuntimeError
would make sense for a Scrapy
version mismatch, while ValueError
may be better if the issue is the
value of a setting.
If your requirement is a minimum Scrapy version, you may use
scrapy.__version__
to enforce your requirement. For example:
from packaging.version import parse as parse_version
import scrapy
class MyComponent:
def __init__(self):
if parse_version(scrapy.__version__) < parse_version("2.7"):
raise RuntimeError(
f"{MyComponent.__qualname__} requires Scrapy 2.7 or "
f"later, which allow defining the process_spider_output "
f"method of spider middlewares as an asynchronous "
f"generator."
)