docs/topics/architecture.rst
.. _topics-architecture:
This document describes the architecture of Scrapy and how its components interact.
The following diagram shows an overview of the Scrapy architecture with its components and an outline of the data flow that takes place inside the system (shown by the red arrows). A brief description of the components is included below with links for more detailed information about them. The data flow is also described below.
.. _data-flow:
.. image:: _images/scrapy_architecture_02.png :width: 700 :height: 470 :alt: Scrapy architecture
The data flow in Scrapy is controlled by the execution engine, and goes like this:
The :ref:Engine <component-engine> gets the initial Requests to crawl from the
:ref:Spider <component-spiders>.
The :ref:Engine <component-engine> schedules the Requests in the
:ref:Scheduler <component-scheduler> and asks for the
next Requests to crawl.
The :ref:Scheduler <component-scheduler> returns the next Requests
to the :ref:Engine <component-engine>.
The :ref:Engine <component-engine> sends the Requests to the
:ref:Downloader <component-downloader>, passing through the
:ref:Downloader Middlewares <component-downloader-middleware> (see
:meth:~scrapy.downloadermiddlewares.DownloaderMiddleware.process_request).
Once the page finishes downloading the
:ref:Downloader <component-downloader> generates a Response (with
that page) and sends it to the Engine, passing through the
:ref:Downloader Middlewares <component-downloader-middleware> (see
:meth:~scrapy.downloadermiddlewares.DownloaderMiddleware.process_response).
The :ref:Engine <component-engine> receives the Response from the
:ref:Downloader <component-downloader> and sends it to the
:ref:Spider <component-spiders> for processing, passing
through the :ref:Spider Middleware <component-spider-middleware> (see
:meth:~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_input).
The :ref:Spider <component-spiders> processes the Response and returns
scraped items and new Requests (to follow) to the
:ref:Engine <component-engine>, passing through the
:ref:Spider Middleware <component-spider-middleware> (see
:meth:~scrapy.spidermiddlewares.SpiderMiddleware.process_spider_output).
The :ref:Engine <component-engine> sends processed items to
:ref:Item Pipelines <component-pipelines>, then send processed Requests to
the :ref:Scheduler <component-scheduler> and asks for possible next Requests
to crawl.
The process repeats (from step 3) until there are no more requests from the
:ref:Scheduler <component-scheduler>.
.. _component-engine:
The engine is responsible for controlling the data flow between all components
of the system, and triggering events when certain actions occur. See the
:ref:Data Flow <data-flow> section above for more details.
.. _component-scheduler:
The :ref:scheduler <topics-scheduler> receives requests from the engine and
enqueues them for feeding them later (also to the engine) when the engine
requests them.
.. _component-downloader:
The Downloader is responsible for fetching web pages and feeding them to the engine which, in turn, feeds them to the spiders.
.. _component-spiders:
Spiders are custom classes written by Scrapy users to parse responses and
extract :ref:items <topics-items> from them or additional requests to
follow. For more information see :ref:topics-spiders.
.. _component-pipelines:
The Item Pipeline is responsible for processing the items once they have been
extracted (or scraped) by the spiders. Typical tasks include cleansing,
validation and persistence (like storing the item in a database). For more
information see :ref:topics-item-pipeline.
.. _component-downloader-middleware:
Downloader middlewares are specific hooks that sit between the Engine and the Downloader and process requests when they pass from the Engine to the Downloader, and responses that pass from Downloader to the Engine.
Use a Downloader middleware if you need to do one of the following:
For more information see :ref:topics-downloader-middleware.
.. _component-spider-middleware:
Spider middlewares are specific hooks that sit between the Engine and the Spiders and are able to process spider input (responses) and output (items and requests).
Use a Spider middleware if you need to
For more information see :ref:topics-spider-middleware.
Scrapy is written with Twisted_, a popular event-driven networking framework
for Python. Thus, it's implemented using a non-blocking (aka asynchronous) code
for concurrency.
For more information about asynchronous programming and Twisted see these links:
twisted:core/howto/defer-introTwisted Introduction - Krondo_.. _Twisted: https://twisted.org/ .. _Twisted Introduction - Krondo: https://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/