Repository navigation
Replies: 3 comments 4 replies
|
For the multi-tab requirement, I would make one long-lived Playwright BrowserContext the boundary of each logical worker, then open that worker's tabs with A few distinctions in Crawlee 3.18.2 matter here:
For restart persistence, use a separate state file per worker, or a separate For a reference starting point, the official multiple-roles example demonstrates explicit context ownership. I'd extend each context with additional tabs before adding crawler scheduling. If you need Crawlee's scheduler, the missing part to design explicitly is worker affinity and what happens when a session/browser is retired; simply increasing concurrency or pool size does not specify that policy. Do your workers need to survive browser restarts, or only keep their tabs isolated during one run? And could you share your current AI-assisted, source-checked against Crawlee v3.18.2 and the linked Playwright documentation; not runtime-tested on your Oracle Linux/Firefox setup. |
|
CC @Mantisus — perhaps you might have an idea how to handle this? |
|
I have a couple of ideas. Neither requires building your own browser context lifecycle, but the second one relies on a few hacks and internal implementation details. I'm not sure yet how much better this can be solved in v4. Crawlee already binds a browser to a session. With The problem to solve is the other direction. One session can end up in several browsers, because a newly launched browser gets a random session from the pool. And a request can go to any browser with a free tab. The simplest option: one crawler per workerGive each worker its own crawler, with its own request queue and a session pool of exactly one session. Once the pool is full, import { PlaywrightCrawler, RequestQueue } from 'crawlee';
import { firefox } from 'playwright';
/**
* One crawler per worker: its own RequestQueue, a SessionPool with a single Session,
* and one Firefox browser whose tabs share one persistent context.
*/
export async function createWorkerCrawler(worker, { tabs, requestHandler, ...options }) {
return new PlaywrightCrawler({
...options,
// A separate queue per worker, so pagination and retries stay with the worker.
requestQueue: await RequestQueue.open(`worker-${worker}`),
launchContext: { launcher: firefox },
useSessionPool: true,
persistCookiesPerSession: true,
sessionPoolOptions: {
// A single Session: every (re)launched browser of this crawler gets the same one back.
maxPoolSize: 1,
persistStateKey: `SESSION_POOL_${worker}`,
// Defaults would rotate the identity after 50 pages, 50 minutes or 3 errors.
sessionOptions: { maxUsageCount: 1_000_000, maxAgeSecs: 86_400, maxErrorScore: 50 },
},
browserPoolOptions: {
maxOpenPagesPerBrowser: tabs,
// Defaults would close the browser after 100 pages or 10 s without a new page.
retireBrowserAfterPageCount: 1_000_000,
retireInactiveBrowserAfterSecs: 3_600,
},
maxConcurrency: tabs,
async requestHandler(context) {
await requestHandler(context);
// Crawlee saves cookies right after navigation; also keep cookies set by JS during the handler.
const { session, browserController, page, request } = context;
session.setCookies(await browserController.getCookies(page), request.loadedUrl);
},
});
}
const startUrls = { w1: 'https://example.com/category/a', w2: 'https://example.com/category/b' };
await Promise.all(
Object.entries(startUrls).map(async ([worker, url]) => {
const crawler = await createWorkerCrawler(worker, {
tabs: 3,
async requestHandler({ enqueueLinks }) {
// ... extract and save products ...
await enqueueLinks({ selector: 'a.next' }); // stays in this worker's queue
},
});
await crawler.run([url]);
}),
);Named queues are not purged on start (only the default storages are), so drop them between runs or run with A single crawler, with
|
Uh oh!
There was an error while loading. Please reload this page.
I'm experimenting with Crawlee on an Oracle Cloud Linux server and could use some advice on the recommended setup.
What we're trying to achieve:
Imagine an airport Wi-Fi network: multiple devices, all sharing one public IP address.
We want to test a similar environment using Crawlee:
Our current setup:
We're using Crawlee JavaScript 3.18.2, Playwright 1.64.1, Node 20, and Firefox.
We've successfully launched two isolated workers with separate cookies on a local test page. However, we're unsure whether we're configuring Crawlee's BrowserPool, SessionPool, and built-in fingerprint features correctly.
A few questions:
We're testing browser-session isolation and concurrency, not looking to circumvent website security restrictions.
Any recommended documentation, examples, or configuration guidance would be appreciated.
Thanks!
All reactions