|
| 1 | +--- |
| 2 | +slug: crawlee-v3-16 |
| 3 | +title: "Crawlee v3.16: AI-Powered Crawling with StagehandCrawler" |
| 4 | +description: "Crawlee v3.16 introduces StagehandCrawler for AI-powered browser automation, async iterators for Dataset and KeyValueStore, sitemap discovery, and improved Cloudflare handling." |
| 5 | +authors: [B4nan] |
| 6 | +--- |
| 7 | + |
| 8 | +Crawlee v3.16 is here, and the headline feature is the new `StagehandCrawler` — an AI-powered crawler that lets you interact with web pages using natural language instead of CSS selectors. On top of that, we've added async iterators for `Dataset` and `KeyValueStore`, a new `discoverValidSitemaps` utility, and made `handleCloudflareChallenge` more configurable. |
| 9 | + |
| 10 | +Here's what's new: |
| 11 | + |
| 12 | +- [StagehandCrawler — AI-powered browser automation](/blog/crawlee-v3-16#stagehandcrawler--ai-powered-browser-automation) |
| 13 | +- [Async iterators for Dataset and KeyValueStore](/blog/crawlee-v3-16#async-iterators-for-dataset-and-keyvaluestore) |
| 14 | +- [discoverValidSitemaps utility](/blog/crawlee-v3-16#discovervalidsitemaps-utility) |
| 15 | +- [Improved Cloudflare challenge handling](/blog/crawlee-v3-16#improved-cloudflare-challenge-handling) |
| 16 | + |
| 17 | +<!-- truncate --> |
| 18 | + |
| 19 | +## StagehandCrawler — AI-powered browser automation |
| 20 | + |
| 21 | +The new [`@crawlee/stagehand`](https://crawlee.dev/js/api/stagehand-crawler) package integrates [Browserbase's Stagehand](https://github.com/browserbase/stagehand) with Crawlee's crawling infrastructure. Instead of writing brittle CSS selectors or XPath expressions, you describe what you want in plain English and let the AI figure out the rest. |
| 22 | + |
| 23 | +The enhanced page object provides four AI methods: |
| 24 | + |
| 25 | +- **`page.act(instruction)`** — perform actions described in natural language (e.g., "Click the 'Load More' button") |
| 26 | +- **`page.extract(instruction, schema)`** — extract structured data from the page using Zod schemas for type safety |
| 27 | +- **`page.observe()`** — discover available actions on the current page |
| 28 | +- **`page.agent(config)`** — create an autonomous agent for complex multi-step workflows |
| 29 | + |
| 30 | +Since [`StagehandCrawler`](https://crawlee.dev/js/api/stagehand-crawler/class/StagehandCrawler) extends [`BrowserCrawler`](https://crawlee.dev/js/api/browser-crawler/class/BrowserCrawler), you get all the standard Crawlee features out of the box — [request queues](https://crawlee.dev/js/docs/guides/request-storage), [proxy rotation](https://crawlee.dev/js/docs/guides/proxy-management), [autoscaling](https://crawlee.dev/js/api/core/class/AutoscaledPool), [session management](https://crawlee.dev/js/docs/guides/session-management), and [browser fingerprinting](https://crawlee.dev/js/docs/guides/avoid-blocking). It's not a separate tool you have to wire up manually; it's a full Crawlee crawler with AI superpowers. |
| 31 | + |
| 32 | +Here's a basic example showing how to interact with a page and extract structured data: |
| 33 | + |
| 34 | +```typescript |
| 35 | +import { StagehandCrawler } from '@crawlee/stagehand'; |
| 36 | +import { z } from 'zod'; |
| 37 | + |
| 38 | +const crawler = new StagehandCrawler({ |
| 39 | + stagehandOptions: { |
| 40 | + model: 'openai/gpt-4.1-mini', |
| 41 | + apiKey: 'your-api-key', // Your OpenAI API key (or use OPENAI_API_KEY env var) |
| 42 | + }, |
| 43 | + async requestHandler({ page, request, log }) { |
| 44 | + log.info(`Processing ${request.url}`); |
| 45 | + |
| 46 | + // Use natural language to interact with the page |
| 47 | + await page.act('Click the "Load More" button'); |
| 48 | + |
| 49 | + // Extract structured data with AI |
| 50 | + const data = await page.extract( |
| 51 | + 'Get all product names and prices', |
| 52 | + z.object({ |
| 53 | + products: z.array(z.object({ |
| 54 | + name: z.string(), |
| 55 | + price: z.number(), |
| 56 | + })), |
| 57 | + }), |
| 58 | + ); |
| 59 | + |
| 60 | + log.info(`Found ${data.products.length} products`); |
| 61 | + }, |
| 62 | +}); |
| 63 | + |
| 64 | +await crawler.run(['https://example.com']); |
| 65 | +``` |
| 66 | + |
| 67 | +The `StagehandCrawler` is especially useful for websites with complex or frequently changing layouts where traditional selectors are hard to maintain. If the target website has a stable structure, [`PlaywrightCrawler`](https://crawlee.dev/js/api/playwright-crawler/class/PlaywrightCrawler) remains the better choice — it's faster and doesn't require AI API keys. |
| 68 | + |
| 69 | +**Installation:** |
| 70 | + |
| 71 | +```bash |
| 72 | +npm install @crawlee/stagehand @browserbasehq/stagehand |
| 73 | +``` |
| 74 | + |
| 75 | +For a deeper dive into the architecture, all four AI methods, configuration options, and more examples, check out the [StagehandCrawler guide](https://crawlee.dev/js/docs/guides/stagehand-crawler-guide). |
| 76 | + |
| 77 | +## Async iterators for Dataset and KeyValueStore |
| 78 | + |
| 79 | +Previously, iterating over all items in a [`Dataset`](https://crawlee.dev/js/api/core/class/Dataset) or all keys in a [`KeyValueStore`](https://crawlee.dev/js/api/core/class/KeyValueStore) required manual pagination with `getData()` or `forEachKey()`. This release adds `for await...of` support, making iteration straightforward and memory-efficient. |
| 80 | + |
| 81 | +Both `Dataset` and `KeyValueStore` now support direct iteration as well as `values()`, `entries()`, and `keys()` methods: |
| 82 | + |
| 83 | +```typescript |
| 84 | +import { Dataset, KeyValueStore } from 'crawlee'; |
| 85 | + |
| 86 | +// Dataset — iterate over all items |
| 87 | +const dataset = await Dataset.open(); |
| 88 | + |
| 89 | +for await (const item of dataset) { |
| 90 | + console.log(item); |
| 91 | +} |
| 92 | + |
| 93 | +// Or use values()/entries() for more control |
| 94 | +for await (const [index, item] of dataset.entries()) { |
| 95 | + console.log(`Item #${index}:`, item); |
| 96 | +} |
| 97 | + |
| 98 | +// KeyValueStore — iterate over entries |
| 99 | +const kvs = await KeyValueStore.open(); |
| 100 | + |
| 101 | +for await (const [key, value] of kvs) { |
| 102 | + console.log(key, value); |
| 103 | +} |
| 104 | + |
| 105 | +// Or iterate over just keys or values |
| 106 | +for await (const key of kvs.keys()) { |
| 107 | + console.log(key); |
| 108 | +} |
| 109 | + |
| 110 | +for await (const value of kvs.values()) { |
| 111 | + console.log(value); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +The iteration handles pagination internally, so you don't have to worry about offsets or cursors. Existing code that uses `await` on `listItems()` or `listKeys()` continues to work unchanged — the methods now return hybrid objects that support both `await` and `for await...of`. |
| 116 | + |
| 117 | +## discoverValidSitemaps utility |
| 118 | + |
| 119 | +The new [`discoverValidSitemaps`](https://crawlee.dev/js/api/utils/function/discoverValidSitemaps) async generator in `@crawlee/utils` takes a list of URLs and automatically discovers sitemap files for those domains. It checks `robots.txt` for sitemap declarations, then tries common paths like `/sitemap.xml`, `/sitemap.txt`, and `/sitemap_index.xml`. |
| 120 | + |
| 121 | +```typescript |
| 122 | +import { discoverValidSitemaps } from '@crawlee/utils'; |
| 123 | + |
| 124 | +for await (const sitemapUrl of discoverValidSitemaps(['https://example.com'])) { |
| 125 | + console.log('Found sitemap:', sitemapUrl); |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +This is handy when you want to seed a crawl from sitemaps without knowing the exact sitemap URL upfront. |
| 130 | + |
| 131 | +## Improved Cloudflare challenge handling |
| 132 | + |
| 133 | +The [`handleCloudflareChallenge`](https://crawlee.dev/js/api/playwright-crawler/namespace/playwrightUtils) helper now accepts configuration callbacks for more control over how Cloudflare challenges are detected and solved. The new options include: |
| 134 | + |
| 135 | +- **`clickPositionCallback`** — override how the checkbox click position is calculated |
| 136 | +- **`clickCallback`** — override the actual checkbox clicking logic |
| 137 | +- **`isChallengeCallback`** — customize detection of Cloudflare challenge pages |
| 138 | +- **`isBlockedCallback`** — customize detection of Cloudflare block pages |
| 139 | +- **`preChallengeSleepSecs`** — add a delay before the first click attempt (defaults to 1s) |
| 140 | + |
| 141 | +```typescript |
| 142 | +import { PlaywrightCrawler } from 'crawlee'; |
| 143 | + |
| 144 | +const crawler = new PlaywrightCrawler({ |
| 145 | + postNavigationHooks: [ |
| 146 | + async ({ handleCloudflareChallenge }) => { |
| 147 | + await handleCloudflareChallenge({ |
| 148 | + // Custom click position for environments where the |
| 149 | + // default detection doesn't work |
| 150 | + clickPositionCallback: async (page) => { |
| 151 | + const box = await page.locator('iframe').first().boundingBox(); |
| 152 | + return box ? { x: box.x + 25, y: box.y + 25 } : null; |
| 153 | + }, |
| 154 | + preChallengeSleepSecs: 2, |
| 155 | + }); |
| 156 | + }, |
| 157 | + ], |
| 158 | + // ... |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +These options are particularly useful when running in environments where the default checkbox detection needs adjustment. |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +That's a wrap for Crawlee v3.16! For the full list of changes, check out the [changelog on GitHub](https://github.com/apify/crawlee/blob/master/CHANGELOG.md). If you have questions or feedback, [open a GitHub discussion](https://github.com/apify/crawlee/discussions) or [join our Discord community](https://apify.com/discord). |
0 commit comments