@crawlee/playwright 4.0.0-beta.326 → 4.0.0-beta.328
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { BrowserHook, LoadedRequest, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
|
|
2
|
-
import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
|
|
2
|
+
import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, ExtractLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
|
|
3
3
|
import { BasicCrawler } from '@crawlee/basic';
|
|
4
4
|
import { CrawlingRequest } from '@crawlee/browser';
|
|
5
|
-
import type { StorageTransactionView } from '@crawlee/core';
|
|
5
|
+
import type { AddRequestsBatchedResult, StorageTransactionView } from '@crawlee/core';
|
|
6
6
|
import type { Dictionary, Awaitable } from '@crawlee/types';
|
|
7
7
|
import { type Cheerio, type CheerioAPI } from 'cheerio';
|
|
8
8
|
import type { AnyNode } from 'domhandler';
|
|
@@ -82,7 +82,18 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
|
|
|
82
82
|
* ```
|
|
83
83
|
*/
|
|
84
84
|
parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
|
|
85
|
-
|
|
85
|
+
/**
|
|
86
|
+
* Extracts URLs from the current page, without adding them to the request queue.
|
|
87
|
+
* In a browser, it first waits for an element matching the selector, with a 5s timeout.
|
|
88
|
+
*/
|
|
89
|
+
extractLinks(options?: ExtractLinksOptions): Promise<string[]>;
|
|
90
|
+
/**
|
|
91
|
+
* Extracts URLs from the current page and adds them to the request queue.
|
|
92
|
+
*
|
|
93
|
+
* By default, the requests are buffered until the crawler settles on this attempt's result, so the returned
|
|
94
|
+
* `requestId` of a newly added request is provisional, and nothing is added when the attempt is discarded.
|
|
95
|
+
*/
|
|
96
|
+
enqueueLinks(options?: EnqueueLinksOptions): Promise<AddRequestsBatchedResult>;
|
|
86
97
|
}
|
|
87
98
|
interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log' | 'extendTimeout'> {
|
|
88
99
|
page?: Page;
|
|
@@ -214,7 +214,7 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
|
|
|
214
214
|
this.#individualRequestHandlerTimeoutMillis = requestHandlerTimeoutSecs * 1000;
|
|
215
215
|
// `renderingTypeDetectionRatio` only configures the default predictor - an injected one brings its own
|
|
216
216
|
// detection ratio (and its own state), so the option is ignored in that case.
|
|
217
|
-
this.#renderingTypePredictor = OwnedOrInjected.resolve(renderingTypePredictor, () => new RenderingTypePredictor({ detectionRatio: renderingTypeDetectionRatio }));
|
|
217
|
+
this.#renderingTypePredictor = OwnedOrInjected.resolve(renderingTypePredictor, () => new RenderingTypePredictor({ detectionRatio: renderingTypeDetectionRatio, id: options.id }));
|
|
218
218
|
this.#attemptWritePolicy = {
|
|
219
219
|
requestQueue: 'deferred',
|
|
220
220
|
...(typeof transactionalStorage === 'object' ? transactionalStorage : {}),
|
|
@@ -319,20 +319,22 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
|
|
|
319
319
|
get parseWithCheerio() {
|
|
320
320
|
throw new Error(errorMessage('parseWithCheerio'));
|
|
321
321
|
},
|
|
322
|
+
get extractLinks() {
|
|
323
|
+
throw new Error(errorMessage('extractLinks'));
|
|
324
|
+
},
|
|
322
325
|
get enqueueLinks() {
|
|
323
326
|
throw new Error(errorMessage('enqueueLinks'));
|
|
324
327
|
},
|
|
325
328
|
}));
|
|
326
329
|
}
|
|
327
330
|
async #adaptCheerioContext(cheerioContext) {
|
|
331
|
+
const extractLinks = async (options = {}) => extractUrlsFromCheerio(cheerioContext.$, options.selector, options.baseUrl ?? cheerioContext.request.loadedUrl);
|
|
328
332
|
return {
|
|
329
333
|
get page() {
|
|
330
334
|
throw new Error('Page object was used in HTTP-only request handler');
|
|
331
335
|
},
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
return (await this.#enqueueLinks(urls, options, cheerioContext.request));
|
|
335
|
-
},
|
|
336
|
+
extractLinks,
|
|
337
|
+
enqueueLinks: async (options = {}) => this.#enqueueLinks(await extractLinks(options), options, cheerioContext.request),
|
|
336
338
|
// `skipNavigation` keeps the inner throwing getter. The body is already consumed (and decoded to UTF-8).
|
|
337
339
|
...(cheerioContext.request.skipNavigation
|
|
338
340
|
? {}
|
|
@@ -365,20 +367,21 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
|
|
|
365
367
|
url: originalResponse.url(),
|
|
366
368
|
});
|
|
367
369
|
}
|
|
370
|
+
const extractLinks = async (options = {}, timeoutMs = 5000) => {
|
|
371
|
+
// TODO consider using `context.parseWithCheerio` to make this universal and avoid code duplication
|
|
372
|
+
const selector = options.selector ?? 'a';
|
|
373
|
+
const locator = playwrightContext.page.locator(selector).first();
|
|
374
|
+
// Wait for client-rendered links, but like the HTTP side, return nothing when there are none.
|
|
375
|
+
await locator.waitFor({ timeout: timeoutMs, state: 'attached' }).catch((error) => {
|
|
376
|
+
if (error.name !== 'TimeoutError')
|
|
377
|
+
throw error;
|
|
378
|
+
});
|
|
379
|
+
return extractUrlsFromPage(playwrightContext.page, selector, options.baseUrl ?? playwrightContext.request.loadedUrl);
|
|
380
|
+
};
|
|
368
381
|
return {
|
|
369
382
|
...(response && { response }),
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
const selector = options.selector ?? 'a';
|
|
373
|
-
const locator = playwrightContext.page.locator(selector).first();
|
|
374
|
-
// Wait for client-rendered links, but like the HTTP side, enqueue nothing when there are none.
|
|
375
|
-
await locator.waitFor({ timeout: timeoutMs, state: 'attached' }).catch((error) => {
|
|
376
|
-
if (error.name !== 'TimeoutError')
|
|
377
|
-
throw error;
|
|
378
|
-
});
|
|
379
|
-
const urls = await extractUrlsFromPage(playwrightContext.page, selector, options.baseUrl ?? playwrightContext.request.loadedUrl);
|
|
380
|
-
return (await this.#enqueueLinks(urls, options, playwrightContext.request));
|
|
381
|
-
},
|
|
383
|
+
extractLinks,
|
|
384
|
+
enqueueLinks: async (options = {}, timeoutMs = 5000) => this.#enqueueLinks(await extractLinks(options, timeoutMs), options, playwrightContext.request),
|
|
382
385
|
};
|
|
383
386
|
}
|
|
384
387
|
/**
|
|
@@ -4,6 +4,12 @@ export type RenderingType = 'clientOnly' | 'static';
|
|
|
4
4
|
export interface RenderingTypePredictorOptions {
|
|
5
5
|
/** A number between 0 and 1 that determines the desired ratio of rendering type detections */
|
|
6
6
|
detectionRatio: number;
|
|
7
|
+
/**
|
|
8
|
+
* Unique identifier for this predictor instance. Used to generate a unique persistence key when
|
|
9
|
+
* `persistenceOptions.persistStateKey` is not provided. If not provided, an auto-incrementing ID is used.
|
|
10
|
+
* Explicit IDs share one namespace with the generated ones, so avoid explicit IDs like `'0'` or `'1'`.
|
|
11
|
+
*/
|
|
12
|
+
id?: string | number;
|
|
7
13
|
persistenceOptions?: Partial<RecoverableStatePersistenceOptions>;
|
|
8
14
|
}
|
|
9
15
|
/**
|
|
@@ -36,7 +42,9 @@ export interface IRenderingTypePredictor {
|
|
|
36
42
|
*/
|
|
37
43
|
export declare class RenderingTypePredictor implements IRenderingTypePredictor {
|
|
38
44
|
#private;
|
|
39
|
-
|
|
45
|
+
/** The identifier used in the default persistence key. */
|
|
46
|
+
readonly id: string;
|
|
47
|
+
constructor({ detectionRatio, id, persistenceOptions }: RenderingTypePredictorOptions);
|
|
40
48
|
/**
|
|
41
49
|
* Initialize the predictor by restoring persisted state.
|
|
42
50
|
*/
|
|
@@ -64,16 +64,20 @@ const stateCodec = z.codec(persistedState, predictorState, {
|
|
|
64
64
|
* Stores rendering type information for previously crawled URLs and predicts the rendering type for URLs that have yet to be crawled and recommends when rendering type detection should be performed.
|
|
65
65
|
*/
|
|
66
66
|
export class RenderingTypePredictor {
|
|
67
|
+
static #nextId = 0;
|
|
68
|
+
/** The identifier used in the default persistence key. */
|
|
69
|
+
id;
|
|
67
70
|
#detectionRatio;
|
|
68
71
|
#state;
|
|
69
|
-
constructor({ detectionRatio, persistenceOptions }) {
|
|
72
|
+
constructor({ detectionRatio, id, persistenceOptions }) {
|
|
73
|
+
this.id = id != null ? String(id) : String(RenderingTypePredictor.#nextId++);
|
|
70
74
|
this.#detectionRatio = detectionRatio;
|
|
71
75
|
this.#state = new RecoverableState({
|
|
72
76
|
defaultState: () => stateCodec.decode({}),
|
|
73
77
|
// The codec validates in the decode direction, so it is a Standard Schema as-is; encoding needs a call.
|
|
74
78
|
deserialize: stateCodec,
|
|
75
79
|
serialize: (state) => stateCodec.encode(state),
|
|
76
|
-
persistStateKey:
|
|
80
|
+
persistStateKey: `CRAWLEE_RENDERING_TYPE_PREDICTOR_STATE_${this.id}`,
|
|
77
81
|
persistenceEnabled: true,
|
|
78
82
|
...persistenceOptions,
|
|
79
83
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/playwright",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.328",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.13.0"
|
|
@@ -49,14 +49,14 @@
|
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@apify/datastructures": "^3.0.1",
|
|
51
51
|
"@apify/timeout": "^1.1.0",
|
|
52
|
-
"@crawlee/basic": "4.0.0-beta.
|
|
53
|
-
"@crawlee/browser": "4.0.0-beta.
|
|
54
|
-
"@crawlee/browser-pool": "4.0.0-beta.
|
|
55
|
-
"@crawlee/cheerio": "4.0.0-beta.
|
|
56
|
-
"@crawlee/core": "4.0.0-beta.
|
|
57
|
-
"@crawlee/http-client": "4.0.0-beta.
|
|
58
|
-
"@crawlee/types": "4.0.0-beta.
|
|
59
|
-
"@crawlee/utils": "4.0.0-beta.
|
|
52
|
+
"@crawlee/basic": "4.0.0-beta.328",
|
|
53
|
+
"@crawlee/browser": "4.0.0-beta.328",
|
|
54
|
+
"@crawlee/browser-pool": "4.0.0-beta.328",
|
|
55
|
+
"@crawlee/cheerio": "4.0.0-beta.328",
|
|
56
|
+
"@crawlee/core": "4.0.0-beta.328",
|
|
57
|
+
"@crawlee/http-client": "4.0.0-beta.328",
|
|
58
|
+
"@crawlee/types": "4.0.0-beta.328",
|
|
59
|
+
"@crawlee/utils": "4.0.0-beta.328",
|
|
60
60
|
"cheerio": "^1.2.0",
|
|
61
61
|
"domhandler": "^6.0.1",
|
|
62
62
|
"jquery": "^3.8.0",
|
|
@@ -81,5 +81,5 @@
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "dfb2fb79cfb0e188e9118a0ec2be614a64732f0a"
|
|
85
85
|
}
|