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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Add proxy guide and fix other stuff (#717)
Ondra Urbanef62220577fd
diff --git a/docs/guides/apify_platform.md b/docs/guides/apify_platform.mdnew file mode 100644index 000000000..148a1e9a2--- /dev/null+++ b/docs/guides/apify_platform.md@@ -0,0 +1,70 @@+---+id: apify-platform+title: Apify Platform+---++Apify is a [platform](https://apify.com) built to serve large scale and high performance web scraping+and automation needs. It provides easy access to [compute instances (Actors)](#what-is-an-actor),+[convenient storages](/docs/guides/data-storage), [proxies](/docs/guides/proxy-management),+[scheduling](https://docs.apify.com/scheduler), [webhooks](https://docs.apify.com/webhooks)+and [more](https://docs.apify.com/), accessible through a [web interface](https://my.apify.com)+or an [API](https://docs.apify.com/api).++While we think that the Apify platform is super cool, and you should definitely try the+[free account](https://my.apify.com/sign-up), **Apify SDK is and will always be open source**,+runnable locally or on any cloud infrastructure.++> Note that we do not test Apify SDK in other cloud environments such as Lambda or on specific+> architectures such as Raspberry PI. We strive to make it work, but there's no guarantee.++## Logging into Apify platform from Apify SDK+To access your [Apify account](https://my.apify.com/sign-up) from the SDK, you must provide+credentials - [your API token](https://my.apify.com/account#/integrations). You can do that+either by utilizing [Apify CLI](https://github.com/apifytech/apify-cli) or by environment+variables.++Once you provide credentials to your scraper, you will be able to use all the Apify platform+features of the SDK, such as calling Actors, saving to cloud storages, using Apify proxies,+setting up webhooks and so on.++### Log in with CLI+Apify CLI allows you to log in to your Apify account on your computer. If you then run your+scraper using the CLI, your credentials will automatically be added.++```+npm install -g apify-cli+```+```+apify login YOUR_API_TOKEN+```+In your project folder:+```+apify run -p+```++### Log in with environment variables+If you prefer not to use Apify CLI, you can always provide credentials to your scraper+by setting the [`APIFY_TOKEN`](/docs/guides/environment-variables#apify_token) environment+variable to your API token.++> There's also the [`APIFY_PROXY_PASSWORD`](/docs/guides/environment-variables#apify_proxy_password)+> environment variable. It is automatically inferred from your token by the SDK, but it can be useful+> when you need to access proxies from a different account than your token represents.++## What is an Actor+When you deploy your script to the Apify platform, it becomes an [actor](https://apify.com/actors).+Actor is a serverless microservice that accepts an input and produces an output. It can run for+a few seconds, hours or even infinitely. An actor can perform anything from a simple action such+as filling out a web form or sending an email, to complex operations such as crawling an entire website+and removing duplicates from a large dataset.++Actors can be shared in the [Apify Store](https://apify.com/store) so that other people can use them.+But don't worry, if you share your actor in the store and somebody uses it, it runs under their account,+not yours.++**Related links**++- [Store of existing actors](https://apify.com/store)+- [Documentation](https://docs.apify.com/actor)+- [View actors in Apify app](https://my.apify.com/actors)+- [API reference](https://apify.com/docs/api/v2#/reference/actors)diff --git a/docs/guides/data_storage.md b/docs/guides/data_storage.mdindex 05058dcfc..aa44a0931 100644--- a/docs/guides/data_storage.md+++ b/docs/guides/data_storage.md@@ -4,17 +4,17 @@ title: Data Storage---The Apify SDK has several data storage types that are useful for specific tasks. The data is stored either on local disk to a directory defined by the-`APIFY_LOCAL_STORAGE_DIR` environment variable, or on the [Apify Cloud](https://apify.com) under the user account+`APIFY_LOCAL_STORAGE_DIR` environment variable, or on the [Apify platform](/docs/guides/apify-platform) under the user accountidentified by the API token defined by the `APIFY_TOKEN` environment variable. If neither of these variables is defined, by default Apify SDK sets`APIFY_LOCAL_STORAGE_DIR` to `./apify_storage` in the current working directory and prints a warning.Typically, you will be developing the code on your local computer and thus set the `APIFY_LOCAL_STORAGE_DIR` environment variable. Once the code is-ready, you will deploy it to the Apify Cloud, where it will automatically set the `APIFY_TOKEN` environment variable and thus use cloud storage. No+ready, you will deploy it to the Apify platform, where it will automatically set the `APIFY_TOKEN` environment variable and thus use cloud storage. Nocode changes are needed.**Related links**-- [Apify cloud storage documentation](https://docs.apify.com/storage)+- [Apify platform storage documentation](https://docs.apify.com/storage)- [View storage in Apify app](https://my.apify.com/storage)- [API reference](https://apify.com/docs/api/v2#/reference/key-value-stores)@@ -73,7 +73,7 @@ Datasets are used to store structured data where each object stored has the samecan imagine a dataset as a table, where each object is a row and its attributes are columns. Dataset is an append-only storage - you can only add newrecords to it but you cannot modify or remove existing records.-When the dataset is stored in the [Apify Cloud](https://apify.com), you can export its data to the following formats: HTML,+When the dataset is stored on the [Apify platform](/docs/guides/apify-platform), you can export its data to the following formats: HTML,JSON, CSV, Excel, XML and RSS. The datasets are displayed on the actor run details page and in the[Storage](https://my.apify.com/storage) section in the Apify app. The actual data is exported using the[Get dataset items](https://apify.com/docs/api/v2#/reference/datasets/item-collection/get-items) Apify API endpoint. Thisdiff --git a/docs/guides/environment_variables.md b/docs/guides/environment_variables.mdindex 32ef3261c..82a6313d6 100644--- a/docs/guides/environment_variables.md+++ b/docs/guides/environment_variables.md@@ -3,41 +3,73 @@ id: environment-variablestitle: Environment Variables----The following is a list of the environment variables used by Apify SDK that are available to the user:+The following is a list of the environment variables used by Apify SDK that are available to the user.+The SDK is capable of running without any env vars present, but certain features will only become available+after env vars are properly set. You can use [Apify CLI](https://github.com/apifytech/apify-cli)+to set the env vars for you. [Apify platform](/docs/guides/apify-platform) also sets the variables automatically.-## `APIFY_HEADLESS`-If set to `1`, web browsers launched by Apify SDK will run in the headless mode. You can still override-this setting in the code, e.g. by passing the `headless: true` option to the-[`Apify.launchPuppeteer()`](/docs/api/apify#launchpuppeteer) function. But having this setting-in an environment variable allows you to develop the crawler locally in headful mode to simplify the debugging,-and only run the crawler in headless mode once you deploy it to the Apify Cloud. By default, the browsers-are launched in headful mode, i.e. with windows.+## Important env vars:+The following environment variables have large impact on the way Apify SDK works and its behavior+can be changed significantly by setting or unsetting them.-## `APIFY_LOCAL_STORAGE_DIR`+### `APIFY_LOCAL_STORAGE_DIR`Defines the path to a local directory where [`KeyValueStore`](/docs/api/key-value-store),[`Dataset`](/docs/api/dataset), and [`RequestQueue`](/docs/api/request-queue) store their data.Typically it is set to `./apify_storage`. If omitted, you should define the [`APIFY_TOKEN`](#apify_token)environment variable instead.-## `APIFY_LOG_LEVEL`+### `APIFY_TOKEN`+The API token for your Apify account. It is used to access the Apify API, e.g. to access cloud storage+or to run an actor on the Apify platform. You can find your API token on the+[Account - Integrations](https://my.apify.com/account#/integrations) page. If omitted,+you should define the `APIFY_LOCAL_STORAGE_DIR` environment variable instead.++### Combinations of `APIFY_LOCAL_STORAGE_DIR` and `APIFY_TOKEN`+By combining the env vars in various ways, you can greatly influence the behavior of Apify SDK.++| Env Vars | API | Storages |+| ------------------------------------------- | --- | -------------- |+| none OR `APIFY_LOCAL_STORAGE_DIR` | no | local |+| `APIFY_TOKEN` | yes | Apify platform |+| `APIFY_TOKEN` AND `APIFY_LOCAL_STORAGE_DIR` | yes | local+platform |++When using both `APIFY_TOKEN` and `APIFY_LOCAL_STORAGE_DIR`, you can use all the Apify platform+features and your data will be stored locally by default. If you want to access platform storages,+you can use the `{ forceCloud: true }` option in their respective functions.++```js+const localDataset = await Apify.openDataset('my-local-data');+const remoteDataset = await Apify.openDataset('my-remote-data', { forceCloud: true });+```++## Convenience env vars:+The next group includes env vars that can help achieve certain goals without having to change+your code, such as temporarily switching log level to DEBUG.++### `APIFY_HEADLESS`+If set to `1`, web browsers launched by Apify SDK will run in the headless mode. You can still override+this setting in the code, e.g. by passing the `headless: true` option to the+[`Apify.launchPuppeteer()`](/docs/api/apify#launchpuppeteer) function. But having this setting+in an environment variable allows you to develop the crawler locally in headful mode to simplify the debugging,+and only run the crawler in headless mode once you deploy it to the Apify platform. By default, the browsers+are launched in headful mode, i.e. with windows.++### `APIFY_LOG_LEVEL`Specifies the minimum log level, which can be one of the following values (in order of severity):`DEBUG`, `INFO`, `WARNING` and `ERROR`. By default, the log level is set to `INFO`,which means that `DEBUG` messages are not printed to console. See the [`utils.log`](/docs/api/log)namespace for logging utilities.-## `APIFY_MEMORY_MBYTES`+### `APIFY_MEMORY_MBYTES`Sets the amount of system memory in megabytes to be used by the [`AutoscaledPool`](/docs/api/autoscaled-pool).It is used to limit the number of concurrently running tasks. By default, the max amount of memoryto be used is set to one quarter of total system memory, i. e. on a system with 8192 MB of memory,the autoscaling feature will only use up to 2048 MB of memory.-## `APIFY_PROXY_PASSWORD`+### `APIFY_PROXY_PASSWORD`Optional password to [Apify Proxy](https://docs.apify.com/proxy) for IP address rotation.-If you have have an Apify Account, you can find the password on the [Proxy page](https://my.apify.com/proxy)-in the Apify app. This feature is optional. You can use your own proxies or no proxies at all.--## `APIFY_TOKEN`-The API token for your Apify Account. It is used to access the Apify API, e.g. to access cloud storage-or to run an actor in the Apify Cloud. You can find your API token on the-[Account - Integrations](https://my.apify.com/account#/integrations) page. If omitted,-you should define the `APIFY_LOCAL_STORAGE_DIR` environment variable instead.+If you have an Apify Account, you can find the password on the [Proxy page](https://my.apify.com/proxy)+in the Apify app. The password is automatically inferred using the `APIFY_TOKEN` env var,+so in most cases, you don't need to touch it. You should use it when, for some reason,+you need access to Apify Proxy, but no access to Apify API, or when you need access to+proxy from a different account than your token represents.diff --git a/docs/guides/getting_started.md b/docs/guides/getting_started.mdindex 76479f850..b5b090c00 100644--- a/docs/guides/getting_started.md+++ b/docs/guides/getting_started.md@@ -17,7 +17,7 @@ The goal of this getting started guide is to provide a step-by-step introductioncreating the simplest of crawlers that only print text to console, all the way up to complex systems that crawl pages, interact with them as if a realuser were sitting in front of a real browser and output structured data.-Since Apify SDK is usable both locally on any computer and on the [Apify Platform](https://my.apify.com), you will be able+Since Apify SDK is usable both locally on any computer and on the [Apify Platform](/docs/guides/apify-platform), you will be ableto use the source code in both environments interchangeably. Nevertheless, some initial setup is still required, so choose your preferred startingenvironment and let's get into it.@@ -45,7 +45,7 @@ npm -vThe fastest and best way to create new projects with the Apify SDK is to use our own[Apify CLI](https://www.npmjs.com/package/apify-cli). This command line tool allows you to create, run and manage Apify-projects with ease, including their deployment to the [Apify Platform](https://my.apify.com) if you wish to run them in the+projects with ease, including their deployment to the [Apify platform](/docs/guides/apify-platform) if you wish to run them in thecloud after developing them locally.Let's install the Apify CLI with the following command:@@ -96,7 +96,7 @@ Did you see all that? If you did, congratulations! You're ready to go!Maybe you don't have Node.js installed and don't want the hassle. Or you can't install anything on your computer because you're using a companyprovided one. Or perhaps you'd just prefer to start working in the cloud right away. Well, no worries, we've got you covered.-The [Apify Platform](https://my.apify.com) is the foundational product of+The [Apify platform](/docs/guides/apify-platform) is the foundational product of[Apify](https://apify.com). It's a serverless cloud computing platform, specifically designed for any web automation jobs,that may include crawling and scraping, but really works amazing for any batch jobs and long running tasks.@@ -105,7 +105,7 @@ haven't already. Don't forget to verify your email. Without it, you won't be ablOnce you're in, you might be prompted by our in-app help to walk through a step-by-step guide into some of our new features. Feel free to finish that,if you'd like, but once you're done, click on the **Actors** tab in the left menu. To read more about **Actors**, see:-[What is an Actor](/docs/guides/what-is-an-actor).+[What is an Actor](/docs/guides/apify-platform#what-is-an-actor).### Creating a new projectdiff --git a/docs/guides/motivation.md b/docs/guides/motivation.mdindex 1e50fd11b..e19dd604d 100644--- a/docs/guides/motivation.md+++ b/docs/guides/motivation.md@@ -9,8 +9,7 @@ eventually things will get complicated. For example, when you try to:- Perform a deep crawl of an entire website using a persistent queue of URLs.- Run your scraping code on a list of 100k URLs in a CSV file, without losing any data when your code crashes.-- Rotate proxies to hide your browser origin.-- Schedule the code to run periodically and send notification on errors.+- Rotate proxies to hide your browser origin and keep user-like sessions.- Disable browser fingerprinting protections used by websites.Python has [Scrapy](https://scrapy.org/) for these tasks, but there was no such library for **JavaScript, the language ofdiff --git a/docs/guides/proxy_management.md b/docs/guides/proxy_management.mdnew file mode 100644index 000000000..22653fd3d--- /dev/null+++ b/docs/guides/proxy_management.md@@ -0,0 +1,143 @@+---+id: proxy-management+title: Proxy Management+---++[IP address blocking](https://en.wikipedia.org/wiki/IP_address_blocking) is one of the oldest+and most effective ways of preventing access to a website. It is therefore paramount for+a good web scraping library to provide easy to use but powerful tools which can work around+IP blocking. The most powerful weapon in your anti IP blocking arsenal is a+[proxy server](https://en.wikipedia.org/wiki/Proxy_server).++With Apify SDK you can use your own proxy servers, proxy servers acquired from+third-party providers, or you can rely on [Apify Proxy](https://apify.com/proxy)+for your scraping needs.++## Quick start+If you already subscribed to Apify Proxy or have proxy URLs of your own, you can start using+them immediately in only a few lines of code.++> If you want to use Apify Proxy, make sure that your [scraper is logged in](/docs/guides/apify-platform).++<!--DOCUSAURUS_CODE_TABS-->++<!-- Apify Proxy -->++```javascript+const proxyConfiguration = await Apify.createProxyConfiguration();+const proxyUrl = proxyConfiguration.newUrl();+```++<!-- Your own proxies -->+```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({+ proxyUrls: [+ 'http://proxy-1.com',+ 'http://proxy-2.com',+ ]+});+const proxyUrl = proxyConfiguration.newUrl();+```++<!--END_DOCUSAURUS_CODE_TABS-->++## Proxy Configuration+All your proxy needs are managed by the [`ProxyConfiguration`](/docs/api/proxy-configuration) class.+You create an instance using the [`Apify.createProxyConfiguration()`](/docs/api/apify#createproxyconfiguration)+function. See the [`ProxyConfigurationOptions`](/docs/typedefs/proxy-configuration-options) for all+the possible constructor options.++### Crawler integration+`ProxyConfiguration` integrates seamlessly into [`CheerioCrawler`](/docs/api/cheerio-crawler)+and [`PuppeteerCrawler`](/docs/api/puppeteer-crawler).++<!--DOCUSAURUS_CODE_TABS-->++<!-- CheerioCrawler -->++```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({ /* your proxy opts */ });+const crawler = new Apify.CheerioCrawler({+ proxyConfiguration,+ // ...+});+```++<!-- PuppeteerCrawler -->+```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({ /* your proxy opts */ });+const crawler = new Apify.PuppeteerCrawler({+ proxyConfiguration,+ // ...+});+```++<!--END_DOCUSAURUS_CODE_TABS-->++Your crawlers will now use the selected proxies for all connections.++### IP Rotation and session management+[`proxyConfiguration.newUrl()`](/docs/api/proxy-configuration#newurl) allows you to pass+a `sessionId` parameter. It will then be used to create a `sessionId`-`proxyUrl` pair,+and subsequent `newUrl()` calls with the same `sessionId` will always return the same+`proxyUrl`. This is extremely useful in scraping, because you want to create the impression+of a real user. See the [session management guide](/docs/guides/session-management) and+[`SessionPool`](/docs/api/session-pool) class for more information on how keeping+a real session helps you avoid blocking.++When no `sessionId` is provided, your proxy URLs are rotated round-robin, whereas+Apify Proxy manages their rotation using black magic to get the best performance.++<!--DOCUSAURUS_CODE_TABS-->++<!-- Standalone -->++```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({ /* opts */ });+const sessionPool = await Apify.openSessionPool({ /* opts */ });+const session = await sessionPool.getSession();+const proxyUrl = proxyConfiguration.newUrl(session.id);+```++<!-- Crawlers -->+```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({ /* opts */ });+const crawler = new Apify.PuppeteerCrawler({+ useSessionPool: true,+ persistCookiesPerSession: true,+ proxyConfiguration,+ // ...+});+```++<!--END_DOCUSAURUS_CODE_TABS-->++## Apify Proxy vs. Your own proxies+The `ProxyConfiguration` class covers both Apify Proxy and custom proxy URLs so that+you can easily switch between proxy providers, however, some features of the class+are available only to Apify Proxy users, mainly because Apify Proxy is what+one would call a super-proxy. It's not a single proxy server, but an API endpoint+that allows connection through millions of different IP addresses. So the class+essentially has two modes: Apify Proxy or Your proxy.++The difference is easy to remember.+[`ProxyConfigurationOptions.proxyUrls`](/docs/typedefs/proxy-configuration-options#proxyurls) and+[`ProxyConfigurationOptions.newUrlFunction`](/docs/typedefs/proxy-configuration-options#newurlfunction)+enable use of your custom proxy URLs, whereas all the other options are there to configure Apify Proxy.+Visit the [Apify Proxy docs](https://docs.apify.com/proxy) for more info on how these parameters work.++## Apify Proxy Configuration+With Apify Proxy, you can select specific proxy groups to use, or countries to connect from.+This allows you to get better proxy performance after some initial research.++```javascript+const proxyConfiguration = await Apify.createProxyConfiguration({+ groups: ['RESIDENTIAL'],+ countryCode: 'US',+});+const proxyUrl = proxyConfiguration.newUrl();+```++Now your crawlers will use only Residential proxies from the US. Note that you must first get access+to a proxy group before you are able to use it. You can find your available proxy groups+in the [proxy dashboard](https://my.apify.com/proxy).diff --git a/docs/guides/puppeteer_live_view.md b/docs/guides/puppeteer_live_view.mddeleted file mode 100644index 719821259..000000000--- a/docs/guides/puppeteer_live_view.md+++ /dev/null@@ -1,37 +0,0 @@-----id: puppeteer-live-view-title: Puppeteer Live View------Apify SDK enables real-time view of launched Puppeteer browser instances and their open tabs, including screenshots of pages and snapshots of HTML.-This is useful for debugging your crawlers that run in headless mode.--The live view dashboard is run on a web server that is started on a port specified by the `APIFY_CONTAINER_PORT` environment variable (typically-4321). To enable live view, pass the `useliveView: true` option to the `puppeteerPoolOptions` of-[`PuppeteerCrawler`](/docs/api/puppeteer-crawler#new-puppeteercrawleroptions):--```js-const crawler = new Apify.PuppeteerCrawler({- puppeteerPoolOptions: {- useLiveView: true,- },- // other options-});-```--or directly to the [`PuppeteerPool`](/docs/api/puppeteer-pool) constructor, when using it standalone:--```js-const pool = new Apify.PuppeteerPool({- useLiveView: true,- // other options-});-```--To simplify debugging, you may also want to add the `{ slowMo: 300 }` option to slow down all browser operations. See-[Puppeteer documentation](https://pptr.dev/#?product=Puppeteer&show=api-puppeteerlaunchoptions) for details.--Once live view is enabled, you can open `http://localhost:4321` and as the crawler runs, you should see screenshots and HTML of the pages it opens-displayed in your browser.--To use live view on the Apify Platform (after enabling it in your actor), simply select the Live View tab in your actor run view.diff --git a/docs/guides/quick_start.md b/docs/guides/quick_start.mdindex 03c90f812..5f6274a2b 100644--- a/docs/guides/quick_start.md+++ b/docs/guides/quick_start.md@@ -57,7 +57,7 @@ By default, Apify SDK stores data to `./apify_storage` in the current working di## Local usage with Apify command-line interface (CLI)To avoid the need to set the environment variables manually, to create a boilerplate of your project, and to enable pushing and running your code on-the [Apify Cloud](https://apify.com), you can use the [Apify command-line interface (CLI)](https://github.com/apifytech/apify-cli) tool.+the [Apify platform](/docs/guides/apify-platform), you can use the [Apify command-line interface (CLI)](https://github.com/apifytech/apify-cli) tool.Install the CLI by running:@@ -84,7 +84,7 @@ apify runBy default, the crawling data will be stored in a local directory at `./apify_storage`. For example, the input JSON file for the actor is expected tobe in the default key-value store in `./apify_storage/key_value_stores/default/INPUT.json`.-Now you can easily deploy your code to the Apify Cloud by running:+Now you can easily deploy your code to the Apify platform by running:```bashapify login@@ -94,12 +94,12 @@ apify loginapify push```-Your script will be uploaded to the Apify Cloud and built there so that it can be run. For more information, view the+Your script will be uploaded to the Apify platform and built there so that it can be run. For more information, view the[Apify Actor](https://docs.apify.com/cli) documentation.-## Usage on the Apify Cloud+## Usage on the Apify platform-You can also develop your web scraping project in an online code editor directly on the [Apify Cloud](https://apify.com).+You can also develop your web scraping project in an online code editor directly on the [Apify platform](/docs/guides/apify-platform).You'll need to have an Apify Account. Go to [Actors](https://my.apify.com/actors), page in the app, click <i>Create new</i>and then go to the <i>Source</i> tab and start writing your code or paste one of the examples from the Examples section.diff --git a/docs/guides/what_is_an_actor.md b/docs/guides/what_is_an_actor.mddeleted file mode 100644index 2d7fa7bd5..000000000--- a/docs/guides/what_is_an_actor.md+++ /dev/null@@ -1,20 +0,0 @@-----id: what-is-an-actor-title: What is an Actor------When you deploy your script to the [Apify Cloud](https://apify.com/), it becomes an actor. An actor is a serverless-microservice that accepts an input and produces an output. It can run for a few seconds, hours or even infinitely. An actor can perform anything from-a simple action such as filling out a web form or sending an email, to complex operations such as crawling an entire website and removing duplicates-from a large dataset.--To run an actor, you need to have an [Apify Account](https://my.apify.com/). Actors can be shared in the-[Apify Store](https://apify.com/store) so that other people can use them. But don't worry, if you share your actor in the-store and somebody uses it, it runs under their account, not yours.--**Related links**--- [Store of existing actors](https://apify.com/store)-- [Documentation](https://docs.apify.com/actor)-- [View actors in Apify app](https://my.apify.com/actors)-- [API reference](https://apify.com/docs/api/v2#/reference/actors)diff --git a/package.json b/package.jsonindex 533c28f88..c48740f66 100644--- a/package.json+++ b/package.json@@ -110,7 +110,7 @@"sinon-stub-promise": "^4.0.0","socket.io-client": "^2.3.0","ts-jest": "^26.0.0",- "typescript": "^3.9.2",+ "typescript": "3.8.3","ua-parser-js": "^0.7.21","why-is-node-running": "^2.1.2"},diff --git a/src/session_pool/session_pool.js b/src/session_pool/session_pool.jsindex 3eef84101..09f374164 100644--- a/src/session_pool/session_pool.js+++ b/src/session_pool/session_pool.js@@ -27,19 +27,35 @@ import { ACTOR_EVENT_NAMES_EX } from '../constants';*//**- * Handles the sessions rotation, creation and persistence.+ * Handles the rotation, creation and persistence of user-like sessions.* Creates a pool of {@link Session} instances, that are randomly rotated.* When some session is marked as blocked. It is removed and new one is created instead.* Learn more in the [`Session management guide`](/docs/guides/session-management).*+ * You can create one by calling the {@link Apify.openSessionPool} function.+ *+ * Session pool is already integrated into crawlers, and it can significantly improve your scraper+ * performance with just 2 lines of code.+ *+ * **Example usage:**+ *+ * ```javascript+ * const crawler = new Apify.CheerioCrawler({+ * useSessionPool: true,+ * persistCookiesPerSession: true,+ * // ...+ * })+ * ```+ *+ * You can configure the pool with many options. See the {@link SessionPoolOptions}.* Session pool is by default persisted in default {@link KeyValueStore}.* If you want to have one pool for all runs you have to specify* {@link SessionPoolOptions.persistStateKeyValueStoreId}.*- * **Example usage:**+ * **Advanced usage:**** ```javascript- * const sessionPool = new SessionPool({+ * const sessionPool = await Apify.openSessionPool({* maxPoolSize: 25,* sessionOptions:{* maxAgeSecs: 10,@@ -49,18 +65,12 @@ import { ACTOR_EVENT_NAMES_EX } from '../constants';* persistStateKey: 'my-session-pool',* });*- * // Now you have to initialize the `SessionPool`.- * // If you already have a persisted state in the selected `KeyValueState`.- * // The Session pool is recreated, otherwise it creates a new one.- * // It also attaches listener to `Apify.events` so it is persisted periodically and not after every change.- * await sessionPool.initialize();- ** // Get random session from the pool* const session1 = await sessionPool.getSession();* const session2 = await sessionPool.getSession();* const session3 = await sessionPool.getSession();*- * // Now you can mark the session either failed of successful+ * // Now you can mark the session either failed or successful** // Marks session as bad after unsuccessful usage -> it increases error count (soft retire)* session1.markBad()@@ -142,7 +152,7 @@ export class SessionPool extends EventEmitter {/*** Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.- * This function must be called before you can start using the instance in a meaningful way.+ * It is called automatically by the {@link Apify.openSessionPool} function.** @return {Promise<void>}*/diff --git a/website/i18n/en.json b/website/i18n/en.jsonindex 34dec73f3..8a4d458b9 100644--- a/website/i18n/en.json+++ b/website/i18n/en.json@@ -146,6 +146,9 @@"examples/synchronous-run": {"title": "Synchronous run"},+ "guides/apify-platform": {+ "title": "Apify Platform"+ },"guides/data-storage": {"title": "Data Storage"},@@ -158,8 +161,8 @@"guides/motivation": {"title": "Motivation"},- "guides/puppeteer-live-view": {- "title": "Puppeteer Live View"+ "guides/proxy-management": {+ "title": "Proxy Management"},"guides/quick-start": {"title": "Quick Start"@@ -170,9 +173,6 @@"guides/type-script-actor": {"title": "TypeScript Actors"},- "guides/what-is-an-actor": {- "title": "What is an Actor"- },"readme/introduction": {"title": "readme/introduction"},diff --git a/website/sidebars.json b/website/sidebars.jsonindex 2983b1cad..da1dc06fc 100644--- a/website/sidebars.json+++ b/website/sidebars.json@@ -3,13 +3,13 @@"Guide": ["guides/motivation","guides/quick-start",+ "guides/apify-platform","guides/getting-started",- "guides/what-is-an-actor","guides/data-storage","guides/environment-variables",+ "guides/proxy-management","guides/session-management",- "guides/type-script-actor",- "guides/puppeteer-live-view"+ "guides/type-script-actor"],"Examples": ["examples/accept-user-input",