You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Unify Apify and Scrapy to use single event loop & remove nest-asyncio (#390)
### Description
- Apify (asyncio) and Scrapy (Twisted) now run on a single event loop.
- `nest-asyncio` has been completely removed.
- It seems that this change also improved the performance.
- The `ApifyScheduler`, which is synchronous, now executes asyncio
coroutines (communication with RQ) in a separate thread with its own
asyncio event loop.
- Logging setup has to be adjusted and I moved to a dedicated file in
the SDK.
- The try-import functionality for optional dependecies from Crawlee was
added to `scrapy` subpackage.
- A new integration test for Scrapy Actor has been added.
### Issues
- Closes: #148
- Closes: #176
- Closes: #392
- Relates: apify/actor-templates#303
- This issue will be closed once the corresponding PR in
`actor-templates` is merged.
### Tests
- A new integration test for Scrapy Actor has been added.
- And of course, it was tested manually using the Actor from
guides/templates.
### Next steps
- Update Scrapy Actor template in `actor-templates`.
- Update [Actor Scrapy Books
Example](https://github.com/apify/actor-scrapy-books-example).
- Add HTTP cache storage for KVS, @honzajavorek will provide his
implementation.
### Follow-up issues
- There are still a few issues to be resolved.
- #391
- #395
[Scrapy](https://scrapy.org/) is an open-source web scraping framework written in Python. It provides a complete set of tools for web scraping, including the ability to define how to extract data from websites, handle pagination and navigation.
16
+
[Scrapy](https://scrapy.org/) is an open-source web scraping framework for Python. It provides tools for defining scrapers, extracting data from web pages, following links, and handling pagination. With the Apify SDK, Scrapy projects can be converted into Apify [Actors](https://docs.apify.com/platform/actors), integrated with Apify [storages](https://docs.apify.com/platform/storage), and executed on the Apify [platform](https://docs.apify.com/platform).
17
17
18
-
:::tip
18
+
## Integrating Scrapy with the Apify platform
19
19
20
-
Our CLI now supports transforming Scrapy projects into Apify Actors with a single command! Check out the [Scrapy migration guide](https://docs.apify.com/cli/docs/integrating-scrapy) for more information.
20
+
The Apify SDK provides an Apify-Scrapy integration. The main challenge of this is to combine two asynchronous frameworks that use different event loop implementations. Scrapy uses [Twisted](https://twisted.org/) for asynchronous execution, while the Apify SDK is based on [asyncio](https://docs.python.org/3/library/asyncio.html). The key thing is to install the Twisted's `asyncioreactor` to run Twisted's asyncio compatible event loop. This allows both Twisted and asyncio to run on a single event loop, enabling a Scrapy spider to run as an Apify Actor with minimal modifications.
21
21
22
-
:::
22
+
<CodeBlockclassName="language-python"title="__main.py__: The Actor entry point ">
23
+
{UnderscoreMainExample}
24
+
</CodeBlock>
23
25
24
-
Some of the key features of Scrapy for web scraping include:
26
+
In this setup, `apify.scrapy.initialize_logging` configures an Apify log formatter and reconfigures loggers to ensure consistent logging across Scrapy, the Apify SDK, and other libraries. The `apify.scrapy.run_scrapy_actor` bridges asyncio coroutines with Twisted's reactor, enabling the Actor's main coroutine, which contains the Scrapy spider, to be executed.
25
27
26
-
-**Request and response handling** - Scrapy provides an easy-to-use interface for making HTTP requests and handling responses,
27
-
allowing you to navigate through web pages and extract data.
28
-
-**Robust Spider framework** - Scrapy has a spider framework that allows you to define how to scrape data from websites,
29
-
including how to follow links, how to handle pagination, and how to parse the data.
30
-
-**Built-in data extraction** - Scrapy includes built-in support for data extraction using XPath and CSS selectors,
31
-
allowing you to easily extract data from HTML and XML documents.
32
-
-**Integration with other tool** - Scrapy can be integrated with other Python tools like BeautifulSoup and Selenium for more advanced scraping tasks.
28
+
Make sure the `SCRAPY_SETTINGS_MODULE` environment variable is set to the path of the Scrapy settings module. This variable is also used by the `Actor` class to detect that the project is a Scrapy project, triggering additional actions.
33
29
34
-
## Using Scrapy template
30
+
<CodeBlockclassName="language-python"title="main.py: The Actor main coroutine">
31
+
{MainExample}
32
+
</CodeBlock>
35
33
36
-
The fastest way to start using Scrapy in Apify Actors is by leveraging the [Scrapy Actor template](https://apify.com/templates/categories/python). This template provides a pre-configured structure and setup necessary to integrate Scrapy into your Actors seamlessly. It includes: setting up the Scrapy settings, `asyncio` reactor, Actor logger, and item pipeline as necessary to make Scrapy spiders run in Actors and save their outputs in Apify datasets.
34
+
Within the Actor's main coroutine, the Actor's input is processed as usual. The function `apify.scrapy.apply_apify_settings` is then used to configure Scrapy settings with Apify-specific components before the spider is executed. The key components and other helper functions are described in the next section.
37
35
38
-
## Manual setup
36
+
## Key integration components
39
37
40
-
If you prefer not to use the template, you will need to manually configure several components to integrate Scrapy with the Apify SDK.
38
+
The Apify SDK provides several custom components to support integration with the Apify platform:
41
39
42
-
### Event loop & reactor
40
+
-[`apify.scrapy.ApifyScheduler`](https://docs.apify.com/sdk/python/reference/class/ApifyScheduler) - Replaces Scrapy's default [scheduler](https://docs.scrapy.org/en/latest/topics/scheduler.html) with one that uses Apify's [request queue](https://docs.apify.com/platform/storage/request-queue) for storing requests. It manages enqueuing, dequeuing, and maintaining the state and priority of requests.
41
+
-[`apify.scrapy.ActorDatasetPushPipeline`](https://docs.apify.com/sdk/python/reference/class/ActorDatasetPushPipeline) - A Scrapy [item pipeline](https://docs.scrapy.org/en/latest/topics/item-pipeline.html) that pushes scraped items to Apify's [dataset](https://docs.apify.com/platform/storage/dataset). When enabled, every item produced by the spider is sent to the dataset.
42
+
-[`apify.scrapy.ApifyHttpProxyMiddleware`](https://docs.apify.com/sdk/python/reference/class/ApifyHttpProxyMiddleware) - A Scrapy [middleware](https://docs.scrapy.org/en/latest/topics/downloader-middleware.html) that manages proxy configurations. This middleware replaces Scrapy's default `HttpProxyMiddleware` to facilitate the use of Apify's proxy service.
43
43
44
-
The Apify SDK is built on Python's asynchronous [`asyncio`](https://docs.python.org/3/library/asyncio.html) library, whereas Scrapy uses [`twisted`](https://twisted.org/) for its asynchronous operations. To make these two frameworks work together, you need to:
44
+
Additional helper functions in the [`apify.scrapy`](https://github.com/apify/apify-sdk-python/tree/master/src/apify/scrapy) subpackage include:
45
45
46
-
- Set the [`AsyncioSelectorReactor`](https://docs.scrapy.org/en/latest/topics/asyncio.html#installing-the-asyncio-reactor) in Scrapy's project settings: This reactor is `twisted`'s implementation of the `asyncio` event loop, enabling compatibility between the two libraries.
47
-
- Install [`nest_asyncio`](https://pypi.org/project/nest-asyncio/): The `nest_asyncio` package allows the asyncio event loop to run within an already running loop, which is essential for integration with the Apify SDK.
46
+
-`apply_apify_settings` - Applies Apify-specific components to Scrapy settings.
47
+
-`to_apify_request` and `to_scrapy_request` - Convert between Apify and Scrapy request objects.
48
+
-`initialize_logging` - Configures logging for the Actor environment.
49
+
-`run_scrapy_actor` - Bridges asyncio and Twisted event loops.
48
50
49
-
By making these adjustments, you can ensure collaboration between `twisted`-based Scrapy and the `asyncio`-based Apify SDK.
51
+
## Create a new Apify-Scrapy project
50
52
51
-
### Other components
53
+
The simplest way to start using Scrapy in Apify Actors is to use the [Scrapy Actor template](https://apify.com/templates/python-scrapy). The template provides a pre-configured project structure and setup that includes all necessary components to run Scrapy spiders as Actors and store their output in Apify datasets. If you prefer manual setup, refer to the example Actor section below for configuration details.
52
54
53
-
We also prepared other Scrapy components to work with Apify SDK, they are available in the [`apify/scrapy`](https://github.com/apify/apify-sdk-python/tree/master/src/apify/scrapy) sub-package. These components include:
55
+
## Wrapping an existing Scrapy project
54
56
55
-
-`ApifyScheduler`: A Scrapy scheduler that uses the Apify Request Queue to manage requests.
56
-
-`ApifyHttpProxyMiddleware`: A Scrapy middleware for working with Apify proxies.
57
-
-`ActorDatasetPushPipeline`: A Scrapy item pipeline that pushes scraped items into the Apify dataset.
57
+
The Apify CLI supports converting an existing Scrapy project into an Apify Actor with a single command. The CLI expects the project to follow the standard Scrapy layout (including a `scrapy.cfg` file in the project root). During the wrapping process, the CLI:
58
58
59
-
The module contains other helper functions, like `apply_apify_settings` for applying these components to Scrapy settings, and `to_apify_request` and `to_scrapy_request` for converting between Apify and Scrapy request objects.
59
+
- Creates the necessary files and directories for an Apify Actor.
60
+
- Installs the Apify SDK and required dependencies.
61
+
- Updates Scrapy settings to include Apify-specific components.
62
+
63
+
For further details, see the [Scrapy migration guide](https://docs.apify.com/cli/docs/integrating-scrapy).
60
64
61
65
## Example Actor
62
66
63
-
Here is an example of a Scrapy Actor that scrapes the titles of web pages and enqueues all links found on each page. This example is identical to the one provided in the Apify Actor templates.
67
+
The following example demonstrates a Scrapy Actor that scrapes page titles and enqueues links found on each page. This example aligns with the structure provided in the Apify Actor templates.
In this guide you learned how to use Scrapy in Apify Actors. You can now start building your own web scraping projects
96
-
using Scrapy, the Apify SDK and host them on the Apify platform. See the [Actor templates](https://apify.com/templates/categories/python) to get started with your own scraping tasks. If you have questions or need assistance, feel free to reach out on our [GitHub](https://github.com/apify/apify-sdk-python) or join our [Discord community](https://discord.com/invite/jyEM2PRvMU). Happy scraping!
99
+
In this guide you learned how to use Scrapy in Apify Actors. You can now start building your own web scraping projects using Scrapy, the Apify SDK and host them on the Apify platform. See the [Actor templates](https://apify.com/templates/categories/python) to get started with your own scraping tasks. If you have questions or need assistance, feel free to reach out on our [GitHub](https://github.com/apify/apify-sdk-python) or join our [Discord community](https://discord.com/invite/jyEM2PRvMU). Happy scraping!
0 commit comments