I want to process requests with label 'search' with a playwright crawler, and process requests with label 'ad' with a cheerio crawler.
The playwright will enqueue all 'ad' requests because the cheerio won't have any start urls.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a solution that uses both PlaywrightCrawler and CheerioCrawler to process different types of requests based on their labels. Here's how you can implement this:
typescript
import { PlaywrightCrawler, CheerioCrawler, createPlaywrightRouter, createCheerioRouter } from 'crawlee';
// Create routers for both crawlers
const playwrightRouter = createPlaywrightRouter();
const cheerioRouter = createCheerioRouter();
// Set up the Playwright router to handle 'search' requests
playwrightRouter.addHandler('search', async ({ page, enqueueLinks, log }) => {
// Handle search pages here
log.info('Processing search page...');
// Find and enqueue ad links with the 'ad' label
await enqueueLinks({
selector: 'a.ad-link', // Replace with your actual ad link selector
label: 'ad', // This will make these requests go to the Cheerio crawler
});
});
// Set up the Cheerio router to handle 'ad' requests
cheerioRouter.addHandler('ad', async ({ $, request, log }) => {
// Handle ad pages here using Cheerio
log.info('Processing ad page...');
const title = $('title').text();
log.info(`Ad title: ${title}`);
});
// Initialize the Playwright crawler
const playwrightCrawler = new PlaywrightCrawler({
requestHandler: playwrightRouter,
});
// Initialize the Cheerio crawler
const cheerioCrawler = new CheerioCrawler({
requestHandler: cheerioRouter,
});
// Run both crawlers
// Start with Playwright crawler for search pages
await playwrightCrawler.run([
{ url: 'http://example.com/search', label: 'search' },
// Add more search URLs as needed
]);
// The Cheerio crawler will process the 'ad' requests that were enqueued by the Playwright crawler
await [Redacted Person].run();
This solution:
Creates two routers using createPlaywrightRouter() and createCheerioRouter() to handle different types of requests
Sets up the Playwright router to handle requests labeled 'search' and enqueue new requests labeled 'ad'
Sets up the Cheerio router to handle requests labeled 'ad' that were enqueued by the Playwright crawler
Creates instances of both crawlers with their respective routers
Runs the crawlers in sequence - first the PlaywrightCrawler processes search pages and enqueues ad URLs, then the CheerioCrawler processes those ad URLs
Note that:
The Cheerio crawler doesn't need any initial URLs since it will process the URLs enqueued by the Playwright crawler
Both crawlers will share the same RequestQueue by default, so requests enqueued by one crawler can be processed by the other
You'll need to adjust the selectors and processing logic in the handlers according to your specific use case
Make sure to handle errors appropriately and adjust the configuration options (like concurrency, timeouts, etc.) based on your needs.