Write a BasicCrawler that executes a PlaywrightCrawler for requests labeled with 'search' and a CheerioCrawler for requests labeled with 'ad' and throw an error for others.
Assistant
User
The requestHandler property is protected and only accessible within class 'BasicCrawler' and its subclasses.
Assistant
User
Give me some github projects that implement this behavior
Assistant
I apologize, but I don't have direct access to search GitHub or browse the internet to find specific project examples. Since my knowledge is limited to the provided repository context files, I cannot point you to specific GitHub projects that implement this behavior.
I'll help you create a BasicCrawler that routes requests to different crawler types based on labels. Here's how we can implement this:
typescript
import { BasicCrawler, PlaywrightCrawler, CheerioCrawler, Request } from 'crawlee';
// Create instances of specialized crawlers
const playwrightCrawler = new PlaywrightCrawler({
async requestHandler({ page, request, pushData }) {
// Handle search pages with Playwright
const data = await page.evaluate(() => {
// Extract data from the page
return {
title: document.title,
url: window.location.href,
};
});
await pushData(data);
},
});
const cheerioCrawler = new CheerioCrawler({
async requestHandler({ $, request, pushData }) {
// Handle ad pages with Cheerio
const data = {
title: $('title').text(),
url: request.url,
};
await pushData(data);
},
});
// Create the main BasicCrawler that will route requests
const crawler = new BasicCrawler({
async requestHandler({ request, log }) {
log.info(`Processing ${request.url}...`);
// Check request label and route to appropriate crawler
if (request.label === 'search') {
await playwrightCrawler.addRequests([request]);
await playwrightCrawler.run();
} else if (request.label === 'ad') {
await cheerioCrawler.addRequests([request]);
await [Redacted Person].run();
} else {
throw new Error(`Unsupported request label: ${request.label}`);
}
},
});
// Add some test requests
await crawler.addRequests([
{ url: 'https://example.com/search', label: 'search' },
{ url: 'https://example.com/ad', label: 'ad' },
]);
// Run the crawler
await crawler.run();
console.log('Crawler finished.');
This implementation:
Creates separate instances of PlaywrightCrawler and CheerioCrawler with their own request handlers
Creates a BasicCrawler that acts as a router
In the BasicCrawler's requestHandler:
Checks the request's label
Routes 'search' labeled requests to PlaywrightCrawler
Routes 'ad' labeled requests to CheerioCrawler
Throws an error for any other labels
Each specialized crawler handles the requests appropriately:
PlaywrightCrawler uses browser automation for search pages
CheerioCrawler uses cheerio for simpler ad pages
You can customize the request handlers for both PlaywrightCrawler and CheerioCrawler based on your specific needs for extracting data from search and ad pages.
Remember to handle errors appropriately and consider adding proper configuration options like concurrency limits, retry settings, and timeouts based on your requirements.
Ah, you're right! We need to extend the BasicCrawler class instead of using it directly. Here's the corrected implementation:
typescript
import { BasicCrawler, PlaywrightCrawler, CheerioCrawler, Request } from 'crawlee';
class RoutingCrawler extends BasicCrawler {
private playwrightCrawler: PlaywrightCrawler;
private [Redacted Person]: [Redacted Person];
constructor() {
super();
this.playwrightCrawler = new PlaywrightCrawler({
async requestHandler({ page, request, pushData }) {
// Handle search pages with Playwright
const data = await page.evaluate(() => {
return {
title: document.title,
url: window.location.href,
};
});
await pushData(data);
},
});
this.cheerioCrawler = new CheerioCrawler({
async requestHandler({ $, request, pushData }) {
// Handle ad pages with Cheerio
const data = {
title: $('title').text(),
url: request.url,
};
await pushData(data);
},
});
}
protected async requestHandler({ request, log }) {
log.info(`Processing ${request.url}...`);
// Check request label and route to appropriate crawler
if (request.label === 'search') {
await this.playwrightCrawler.addRequests([request]);
await this.playwrightCrawler.run();
} else if (request.label === 'ad') {
await this.cheerioCrawler.addRequests([request]);
await this.cheerioCrawler.run();
} else {
throw new Error(`Unsupported request label: ${request.label}`);
}
}
}
// Create and use the crawler
const crawler = new RoutingCrawler();
// Add test requests
await crawler.addRequests([
{ url: 'https://example.com/search', label: 'search' },
{ url: 'https://example.com/ad', label: 'ad' },
]);
// Run the crawler
await crawler.run();
console.log('Crawler finished.');
The key changes in this version:
Created a new RoutingCrawler class that extends BasicCrawler
Moved the PlaywrightCrawler and CheerioCrawler instances to be class properties
Implemented the protected requestHandler method within the class
Initialized the specialized crawlers in the constructor
This implementation properly respects the protected nature of the requestHandler property while maintaining the same routing functionality. The crawler will still:
Use PlaywrightCrawler for requests labeled 'search'
Use CheerioCrawler for requests labeled 'ad'
Throw an error for any other labels
You can now create instances of this RoutingCrawler class and use it to handle your requests appropriately.
Here's a more complete and practical example that demonstrates routing between different crawler types: