Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Add logging to capture weird behavior of Statistics

Ondra Urban9942195da151
diff --git a/CHANGELOG.md b/CHANGELOG.md
index decac0dbe..3466705ae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -44,8 +44,6 @@ Full list of changes:
- **BREAKING:** Removed `PuppeteerCrawlerOptions.launchPuppeteerOptions`. Use `launchContext`.
- **BREAKING:** Removed `PuppeteerCrawlerOptions.launchPuppeteerFunction`.
Use `PuppeteerCrawlerOptions.preLaunchHooks` and `postLaunchHooks`.
-- **BREAKING:** Removed `PuppeteerCrawlerOptions.gotoFunction`.
- Use `PuppeteerCrawlerOptions.preNavigationHooks` and `postNavigationHooks`.
- **BREAKING:** Removed `args.autoscaledPool` and `args.puppeteerPool` from `handle(Page/Request)Function`
arguments. Use `args.crawler.autoscaledPool` and `args.crawler.browserPool`.
- **BREAKING:** The `useSessionPool` and `persistCookiesPerSession` options of crawlers
@@ -53,6 +51,9 @@ Full list of changes:
- **BREAKING:** `Apify.launchPuppeteer()` no longer accepts `LaunchPuppeteerOptions`.
It now accepts `LaunchContext`.
+- **DEPRECATED:** `PuppeteerCrawlerOptions.gotoFunction`.
+ Use `PuppeteerCrawlerOptions.preNavigationHooks` and `postNavigationHooks`.
+
- Added `Apify.PlaywrightCrawler` which is almost identical to `PuppeteerCrawler`,
but it crawls with the `playwright` library.
- Added `Apify.launchPlaywright(launchContext)` helper function.
diff --git a/MIGRATIONS.md b/MIGRATIONS.md
index 8b8c6d018..6d8be9c7d 100644
--- a/MIGRATIONS.md
+++ b/MIGRATIONS.md
@@ -3,12 +3,16 @@ There are a lot of breaking changes in the v1.0.0 release, but we're confident t
updating your code will be a matter of minutes. Below, you'll find examples how to do it
and also short tutorials how to use many of the new features.
+If you hadn't yet, we suggest reading the [CHANGELOG](https://github.com/apify/apify-js/blob/master/CHANGELOG.md)
+for a high level view of the changes.
+
> Many of the new features are made with power users in mind,
> so don't worry if something looks complicated. You don't need to use it.
<!-- toc -->
- [Installation](#installation)
+- [Running on Apify Platform](#running-on-apify-platform)
- [Handler arguments are now Crawling Context](#handler-arguments-are-now-crawling-context)
* [`Map` of crawling contexts and their IDs](#map-of-crawling-contexts-and-their-ids)
* [`autoscaledPool` was moved under `crawlingContext.crawler`](#autoscaledpool-was-moved-under-crawlingcontextcrawler)
@@ -31,7 +35,7 @@ and also short tutorials how to use many of the new features.
## Installation
Previous versions of the SDK bundled the `puppeteer` package, so you did not have to install
it. SDK v1 supports also `playwright` and we don't want to force users to install both.
-To install SDK v1 with Pupppeteer (same as previous versions), run:
+To install SDK v1 with Puppeteer (same as previous versions), run:
```bash
npm install apify puppeteer
@@ -42,6 +46,19 @@ To install SDK v1 with Playwright run:
npm install apify playwright
```
+> While we tried to add the most important functionality in the initial release,
+> you may find that there are still some utilities or options that are only
+> supported by Puppeteer and not Playwright.
+
+## Running on Apify Platform
+If you want to make use of Playwright on the Apify Platform, you need to use a Docker image
+that supports Playwright. We've created one for you, but note that it includes both
+Puppeteer and Playwright and all their browsers, so the image size is quite large.
+
+```Dockerfile
+FROM apify/actor-node-playwright
+```
+
## Handler arguments are now Crawling Context
Previously, arguments of user provided handler functions were provided in separate
objects. This made it difficult to track values across function invocations.
@@ -62,16 +79,16 @@ This happened because a new arguments object was created for each function.
With SDK v1 we now have a single object called Crawling Context.
```js
-const handlePageFunction = async (handlePageContext) => {
- handlePageContext.hasOwnProperty('proxyInfo') // true
+const handlePageFunction = async (crawlingContext1) => {
+ crawlingContext1.hasOwnProperty('proxyInfo') // true
}
-const handleFailedRequestFunction = async (handleFailedContext) => {
- handleFailedContext.hasOwnProperty('proxyInfo') // true
+const handleFailedRequestFunction = async (crawlingContext2) => {
+ crawlingContext2.hasOwnProperty('proxyInfo') // true
}
// All contexts are the same object.
-handlePageContext === handleFailedContext // true
+crawlingContext1 === crawlingContext2 // true
```
### `Map` of crawling contexts and their IDs
diff --git a/README.md b/README.md
index 718bffd8c..e575b7100 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,9 @@
[![npm version](https://badge.fury.io/js/apify.svg)](https://www.npmjs.com/package/apify)
[![Build Status](https://travis-ci.com/apify/apify-js.svg?branch=master)](https://travis-ci.com/apify/apify-js)
+> This README is for the soon to be relased Apify SDK v1.0.0. [Find the 0.22.x README here](https://github.com/apify/apify-js/tree/v0.22.3).
+> You can try the beta of SDK v1 by installing `apify@beta`.
+
Apify SDK simplifies the development of web crawlers, scrapers, data extractors and web automation jobs.
It provides tools to manage and automatically scale a pool of headless browsers,
to maintain queues of URLs to crawl, store crawling results to a local filesystem or into the cloud,
diff --git a/src/crawlers/statistics.js b/src/crawlers/statistics.js
index e05245e6e..545d5c19f 100644
--- a/src/crawlers/statistics.js
+++ b/src/crawlers/statistics.js
@@ -253,6 +253,15 @@ class Statistics {
if (!savedState) return;
+ // We saw a run where the requestRetryHistogram was not iterable and crashed
+ // the actor. Adding some logging to monitor this problem in the future.
+ if (!Array.isArray(savedState.requestRetryHistogram)) {
+ this.log.warning('Received invalid state from Key-value store.', {
+ persistStateKey: this.persistStateKey,
+ state: savedState,
+ });
+ }
+
this.log.debug('Recreating state from KeyValueStore', { persistStateKey: this.persistStateKey });
this.requestRetryHistogram.push(...savedState.requestRetryHistogram);