Crawlee + Playwright + Chrome
Web scraper example with Crawlee, Playwright and headless Chrome. Playwright is more modern, user-friendly and harder to block than Puppeteer.
src/main.ts
src/routes.ts
1/**
2 * This template is a production ready boilerplate for developing with `PlaywrightCrawler`.
3 * Use this to bootstrap your projects using the most up-to-date code.
4 * If you're looking for examples or want to learn more, see README.
5 */
6
7// For more information, see https://docs.apify.com/sdk/js
8import { Actor } from 'apify';
9// For more information, see https://crawlee.dev
10import { PlaywrightCrawler } from 'crawlee';
11// this is ESM project, and as such, it requires you to specify extensions in your relative imports
12// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
13// note that we need to use `.js` even when inside TS files
14import { router } from './routes.js';
15
16interface Input {
17 startUrls: string[];
18 maxRequestsPerCrawl: number;
19}
20
21// Initialize the Apify SDK
22await Actor.init();
23
24// Structure of input is defined in input_schema.json
25const {
26 startUrls = ['https://crawlee.dev'],
27 maxRequestsPerCrawl = 100,
28} = await Actor.getInput<Input>() ?? {} as Input;
29
30const proxyConfiguration = await Actor.createProxyConfiguration();
31
32const crawler = new PlaywrightCrawler({
33 proxyConfiguration,
34 maxRequestsPerCrawl,
35 requestHandler: router,
36 launchContext: {
37 launchOptions: {
38 args: [
39 '--disable-gpu', // Mitigates the "crashing GPU process" issue in Docker containers
40 ]
41 }
42 }
43});
44
45await crawler.run(startUrls);
46
47// Exit successfully
48await Actor.exit();
PlaywrightCrawler template
This template is a production ready boilerplate for developing an Actor with PlaywrightCrawler
. Use this to bootstrap your projects using the most up-to-date code.
We decided to split Apify SDK into two libraries, Crawlee and Apify SDK v3. Crawlee will retain all the crawling and scraping-related tools and will always strive to be the best web scraping library for its community. At the same time, Apify SDK will continue to exist, but keep only the Apify-specific features related to building actors on the Apify platform. Read the upgrading guide to learn about the changes.
Resources
If you're looking for examples or want to learn more visit:
- Crawlee + Apify Platform guide
- Documentation and examples
- Node.js tutorials in Academy
- Scraping single-page applications with Playwright
- How to scale Puppeteer and Playwright
- Integration with Zapier, Make, GitHub, Google Drive and other apps
- Video guide on getting scraped data using Apify API
- A short guide on how to build web scrapers using code templates:
Scrape single page with provided URL with Axios and extract data from page's HTML with Cheerio.
A scraper example that uses Cheerio to parse HTML. It's fast, but it can't run the website's JavaScript or pass JS anti-scraping challenges.
Example of a Puppeteer and headless Chrome web scraper. Headless browsers render JavaScript and are harder to block, but they're slower than plain HTTP.
Example of using the Playwright Test project to run automated website tests in the cloud and display their results. Usable as an API.
Empty template with basic structure for the Actor with Apify SDK that allows you to easily add your own functionality.
Template with basic structure for an Actor using Standby mode that allows you to easily add your own functionality.