Skip to content

Commit 3bf64a9

Browse files
authored
docs: snippets: update imports, add Crawlee, Actor import, etc (#425)
- update imports to ESM, add Crawlee where needed, add Actor import - ditch Node.js/got proxy examples
1 parent 50afbf2 commit 3bf64a9

20 files changed

+725
-719
lines changed

content/docs/about.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ Apify is an online platform specializing in [web scraping]({{@link web_scraping_
1313

1414
[Apify Store](https://apify.com/store) includes a range of ready-made tools such as **Booking Scraper** ([dtrungtin/booking-scraper](https://apify.com/dtrungtin/booking-scraper)), **SEO Audit Tool** ([drobnikj/seo-audit-tool](https://apify.com/drobnikj/seo-audit-tool)), or the **Google Sheets Import & Export** ([lukaskrivka/google-sheets](https://apify.com/lukaskrivka/google-sheets)) tool. You can try all of these for free right now.
1515

16-
If your needs are more specific, you can order a [custom solution](https://apify.com/custom-solutions) from [us](https://apify.com/enterprise), our partners or [Apify-approved developers](https://apify.com/marketplace).
16+
If your needs are more specific, you can order a [custom solution](https://apify.com/custom-solutions) from [us](https://apify.com/enterprise), our partners or [Apify-approved developers](https://apify.com/partners/freelancers).
1717

1818
## [](#get-started) Get started
1919

2020
Check out [this video](https://www.youtube.com/watch?v=BsidLZKdYWQ&t=115s) on how to use our **Amazon Scraper** ([vaclavrut/amazon-crawler](https://apify.com/vaclavrut/amazon-crawler)). It introduces how web scraping can help your business and serves as a foundation on how to start using other ready-made actors.
2121

22-
For an overview of all of Apify's features, take a [tour of the platform](https://www.youtube.com/watch?v=XPF0kbyvoOs).
22+
For an overview of all of Apify's features, take a [tour of the platform](https://www.youtube.com/watch?v=nn-bCRvhNUM).
2323

2424
### [](#tutorials) Tutorials
2525

content/docs/actors/development/source_code.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,30 @@ To make you actor compatible with metamorph operation use `Apify.getInput()` ins
168168
For example, imagine you have an actor that accepts a hotel URL on input and then internally uses the [apify/web-scraper](https://www.apify.com/apify/web-scraper) actor to scrape all the hotel reviews. The metamorphing code would look as follows:
169169

170170
```js
171-
const Apify = require('apify');
172-
173-
Apify.main(async () => {
174-
// Get input of your actor.
175-
const { hotelUrl } = await Apify.getInput();
176-
177-
// Create input for apify/web-scraper
178-
const newInput = {
179-
startUrls: [{ url: hotelUrl }],
180-
pageFunction: () => {
181-
// Here you pass the page function that
182-
// scrapes all the reviews ...
183-
},
184-
// ... and here would be all the additional
185-
// input parameters.
186-
};
187-
188-
// Transform the actor run to apify/web-scraper
189-
// with the new input.
190-
await Apify.metamorph('apify/web-scraper', newInput);
191-
192-
// The line here will never be reached, because the
193-
// actor run will be interrupted.
194-
});
171+
import { Actor } from 'apify';
172+
173+
await Actor.init();
174+
175+
// Get input of your actor.
176+
const { hotelUrl } = await Actor.getInput();
177+
178+
// Create input for apify/web-scraper
179+
const newInput = {
180+
startUrls: [{ url: hotelUrl }],
181+
pageFunction: () => {
182+
// Here you pass the page function that
183+
// scrapes all the reviews ...
184+
},
185+
// ... and here would be all the additional
186+
// input parameters.
187+
};
188+
189+
// Transform the actor run to apify/web-scraper
190+
// with the new input.
191+
await Actor.metamorph('apify/web-scraper', newInput);
192+
193+
// The line here will never be reached, because the
194+
// actor run will be interrupted.
195+
196+
await Actor.exit();
195197
```

content/docs/actors/development/state_persistence.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,29 @@ The [Apify SDK](https://sdk.apify.com) persists its state automatically, using t
4343
To persist state manually, you can use the [Apify.events](https://docs.apify.com/actors/development/state-persistence) method in the Apify SDK.
4444

4545
```javascript
46-
Apify.events.on('migrating', () => {
47-
Apify.setValue('my-crawling-state', {
46+
import { Actor } from 'apify';
47+
48+
await Actor.init();
49+
// ...
50+
Actor.on('migrating', () => {
51+
Actor.setValue('my-crawling-state', {
4852
foo: 'bar',
4953
});
5054
});
55+
// ...
56+
await Actor.exit();
5157
```
5258

5359
To check for state saved in a previous run, use:
5460

5561
```javascript
56-
const previousCrawlingState = await Apify.getValue('my-crawling-state') || {};
62+
import { Actor } from 'apify';
63+
64+
await Actor.init();
65+
// ...
66+
const previousCrawlingState = await Actor.getValue('my-crawling-state') || {};
67+
// ...
68+
await Actor.exit();
5769
```
5870

5971
To improve your actor's performance, you can also [cache repeated page data]({{@link tutorials/cache_data_to_improve_performance.md}}).

content/docs/actors/running.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ Actors can also be invoked programmatically from other actors:
5151
```marked-tabs
5252
5353
<marked-tab header="NodeJS" lang="javascript">
54+
import { Actor } from 'apify';
5455
55-
const run = await Apify.call('apify/hello-world', {
56+
await Actor.init();
57+
// ...
58+
const run = await Actor.call('apify/hello-world', {
5659
message: 'Hello!',
5760
});
5861
console.dir(run.output);
59-
62+
// ...
63+
await Actor.exit();
6064
</marked-tab>
6165
6266
@@ -133,8 +137,10 @@ The web server running inside the container must listen at the port defined by t
133137
The following example demonstrates how to start a simple web server in your actor:
134138

135139
```js
136-
const Apify = require('apify');
137-
const express = require('express');
140+
import { Actor } from 'apify';
141+
import express from 'express';
142+
143+
await Actor.init();
138144

139145
const app = express();
140146
const port = process.env.APIFY_CONTAINER_PORT;
@@ -147,10 +153,10 @@ app.listen(port, () => console.log(`Web server is listening
147153
and can be accessed at
148154
${process.env.APIFY_CONTAINER_URL}!`));
149155

150-
Apify.main(async () => {
151-
// Let the actor run for an hour.
152-
await Apify.utils.sleep(60 * 60 * 1000);
153-
});
156+
// Let the actor run for an hour.
157+
await new Promise(r => setTimeout(r, 60 * 60 * 1000));
158+
159+
await Actor.exit();
154160
```
155161

156162
## Data retention

0 commit comments

Comments
 (0)