@crawlee/playwright 4.0.0-beta.10 → 4.0.0-beta.101
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.
- package/README.md +17 -13
- package/index.d.ts +1 -2
- package/index.js +0 -1
- package/internals/adaptive-playwright-crawler.d.ts +117 -63
- package/internals/adaptive-playwright-crawler.js +357 -212
- package/internals/enqueue-links/click-elements.d.ts +37 -55
- package/internals/enqueue-links/click-elements.js +51 -43
- package/internals/playwright-crawler.d.ts +107 -86
- package/internals/playwright-crawler.js +87 -43
- package/internals/playwright-launcher.d.ts +6 -5
- package/internals/playwright-launcher.js +10 -11
- package/internals/utils/playwright-utils.d.ts +62 -27
- package/internals/utils/playwright-utils.js +100 -87
- package/internals/utils/rendering-type-prediction.d.ts +27 -12
- package/internals/utils/rendering-type-prediction.js +67 -26
- package/package.json +17 -11
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/internals/adaptive-playwright-crawler.d.ts.map +0 -1
- package/internals/adaptive-playwright-crawler.js.map +0 -1
- package/internals/enqueue-links/click-elements.d.ts.map +0 -1
- package/internals/enqueue-links/click-elements.js.map +0 -1
- package/internals/playwright-crawler.d.ts.map +0 -1
- package/internals/playwright-crawler.js.map +0 -1
- package/internals/playwright-launcher.d.ts.map +0 -1
- package/internals/playwright-launcher.js.map +0 -1
- package/internals/utils/playwright-utils.d.ts.map +0 -1
- package/internals/utils/playwright-utils.js.map +0 -1
- package/internals/utils/rendering-type-prediction.d.ts.map +0 -1
- package/internals/utils/rendering-type-prediction.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook,
|
|
2
|
-
import { BrowserCrawler
|
|
3
|
-
import type {
|
|
1
|
+
import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook, GetUserDataFromRequest, RequestHandler, RouterHandler, RouterRoutes, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
|
|
2
|
+
import { BrowserCrawler } from '@crawlee/browser';
|
|
3
|
+
import type { PlaywrightPlugin } from '@crawlee/browser-pool';
|
|
4
4
|
import type { Dictionary } from '@crawlee/types';
|
|
5
5
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
6
|
-
import type { LaunchOptions, Page, Response } from 'playwright';
|
|
6
|
+
import type { Download, LaunchOptions, Page, Response } from 'playwright';
|
|
7
|
+
import type { EnqueueLinksByClickingElementsOptions } from './enqueue-links/click-elements.js';
|
|
7
8
|
import type { PlaywrightLaunchContext } from './playwright-launcher.js';
|
|
8
|
-
import type { DirectNavigationOptions, PlaywrightContextUtils } from './utils/playwright-utils.js';
|
|
9
|
-
export
|
|
9
|
+
import type { BlockRequestsOptions, DirectNavigationOptions, HandleCloudflareChallengeOptions, InfiniteScrollOptions, InjectFileOptions, PlaywrightContextUtils, SaveSnapshotOptions } from './utils/playwright-utils.js';
|
|
10
|
+
export type PlaywrightGotoOptions = NonNullable<Parameters<Page['goto']>[1]>;
|
|
11
|
+
export interface PlaywrightCrawlingContext<UserData extends Dictionary = Dictionary> extends BrowserCrawlingContext<Page, Response, UserData, PlaywrightGotoOptions>, PlaywrightContextUtils {
|
|
10
12
|
}
|
|
11
13
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
12
|
-
export interface PlaywrightHook extends BrowserHook<PlaywrightCrawlingContext
|
|
14
|
+
export interface PlaywrightHook extends BrowserHook<PlaywrightCrawlingContext> {
|
|
13
15
|
}
|
|
14
|
-
export interface
|
|
15
|
-
}
|
|
16
|
-
export type PlaywrightGotoOptions = Parameters<Page['goto']>[1];
|
|
17
|
-
export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<PlaywrightCrawlingContext, {
|
|
16
|
+
export interface PlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends PlaywrightCrawlingContext = PlaywrightCrawlingContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<PlaywrightCrawlingContext['request']>>> extends BrowserCrawlerOptions<Page, Response, PlaywrightCrawlingContext, ContextExtension, ExtendedContext, {
|
|
18
17
|
browserPlugins: [PlaywrightPlugin];
|
|
19
|
-
}> {
|
|
18
|
+
}, Routes> {
|
|
20
19
|
/**
|
|
21
20
|
* The same options as used by {@link launchPlaywright}.
|
|
22
21
|
*/
|
|
@@ -28,33 +27,6 @@ export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<Playwrig
|
|
|
28
27
|
* - `request` is an instance of the {@link Request} object with details about the URL to open, HTTP method etc.
|
|
29
28
|
* - `page` is an instance of the `Playwright`
|
|
30
29
|
* [`Page`](https://playwright.dev/docs/api/class-page)
|
|
31
|
-
* - `browserController` is an instance of the
|
|
32
|
-
* [`BrowserController`](https://github.com/apify/browser-pool#browsercontroller),
|
|
33
|
-
* - `response` is an instance of the `Playwright`
|
|
34
|
-
* [`Response`](https://playwright.dev/docs/api/class-response),
|
|
35
|
-
* which is the main resource response as returned by `page.goto(request.url)`.
|
|
36
|
-
*
|
|
37
|
-
* The function must return a promise, which is then awaited by the crawler.
|
|
38
|
-
*
|
|
39
|
-
* If the function throws an exception, the crawler will try to re-crawl the
|
|
40
|
-
* request later, up to `option.maxRequestRetries` times.
|
|
41
|
-
* If all the retries fail, the crawler calls the function
|
|
42
|
-
* provided to the `failedRequestHandler` parameter.
|
|
43
|
-
* To make this work, you should **always**
|
|
44
|
-
* let your function throw exceptions rather than catch them.
|
|
45
|
-
* The exceptions are logged to the request using the
|
|
46
|
-
* {@link Request.pushErrorMessage} function.
|
|
47
|
-
*/
|
|
48
|
-
requestHandler?: PlaywrightRequestHandler;
|
|
49
|
-
/**
|
|
50
|
-
* Function that is called to process each request.
|
|
51
|
-
*
|
|
52
|
-
* The function receives the {@link PlaywrightCrawlingContext} as an argument, where:
|
|
53
|
-
* - `request` is an instance of the {@link Request} object with details about the URL to open, HTTP method etc.
|
|
54
|
-
* - `page` is an instance of the `Playwright`
|
|
55
|
-
* [`Page`](https://playwright.dev/docs/api/class-page)
|
|
56
|
-
* - `browserController` is an instance of the
|
|
57
|
-
* [`BrowserController`](https://github.com/apify/browser-pool#browsercontroller),
|
|
58
30
|
* - `response` is an instance of the `Playwright`
|
|
59
31
|
* [`Response`](https://playwright.dev/docs/api/class-response),
|
|
60
32
|
* which is the main resource response as returned by `page.goto(request.url)`.
|
|
@@ -69,32 +41,29 @@ export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<Playwrig
|
|
|
69
41
|
* let your function throw exceptions rather than catch them.
|
|
70
42
|
* The exceptions are logged to the request using the
|
|
71
43
|
* {@link Request.pushErrorMessage} function.
|
|
72
|
-
*
|
|
73
|
-
* @deprecated `handlePageFunction` has been renamed to `requestHandler` and will be removed in a future version.
|
|
74
|
-
* @ignore
|
|
75
44
|
*/
|
|
76
|
-
|
|
45
|
+
requestHandler?: RouterHandler<ExtendedContext, Routes> | RequestHandler<ExtendedContext>;
|
|
77
46
|
/**
|
|
78
47
|
* Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies
|
|
79
|
-
* or browser properties before navigation. The function
|
|
80
|
-
*
|
|
48
|
+
* or browser properties before navigation. The function receives the `crawlingContext`; the options object
|
|
49
|
+
* forwarded to `page.goto()` is available as `crawlingContext.gotoOptions` and can be mutated in place.
|
|
50
|
+
* A hook may optionally return a partial object whose properties are merged into the crawling context
|
|
51
|
+
* (e.g. to override context members for subsequent hooks and pipeline stages).
|
|
81
52
|
* Example:
|
|
82
53
|
* ```
|
|
83
54
|
* preNavigationHooks: [
|
|
84
|
-
* async (
|
|
85
|
-
* const { page } = crawlingContext;
|
|
55
|
+
* async ({ page, gotoOptions }) => {
|
|
86
56
|
* await page.evaluate((attr) => { window.foo = attr; }, 'bar');
|
|
57
|
+
* gotoOptions.timeout = 60_000;
|
|
87
58
|
* },
|
|
88
59
|
* ]
|
|
89
60
|
* ```
|
|
90
|
-
*
|
|
91
|
-
* Modyfing `pageOptions` is supported only in Playwright incognito.
|
|
92
|
-
* See {@link PrePageCreateHook}
|
|
93
61
|
*/
|
|
94
|
-
preNavigationHooks?:
|
|
62
|
+
preNavigationHooks?: BrowserHook<PlaywrightCrawlingContext, ContextExtension>[];
|
|
95
63
|
/**
|
|
96
64
|
* Async functions that are sequentially evaluated after the navigation. Good for checking if the navigation was successful.
|
|
97
|
-
* The function accepts `crawlingContext` as the only parameter.
|
|
65
|
+
* The function accepts `crawlingContext` as the only parameter. A hook may optionally return a partial object
|
|
66
|
+
* whose properties are merged into the crawling context (e.g. to override `response` after solving a challenge).
|
|
98
67
|
* Example:
|
|
99
68
|
* ```
|
|
100
69
|
* postNavigationHooks: [
|
|
@@ -107,7 +76,7 @@ export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<Playwrig
|
|
|
107
76
|
* ]
|
|
108
77
|
* ```
|
|
109
78
|
*/
|
|
110
|
-
postNavigationHooks?:
|
|
79
|
+
postNavigationHooks?: BrowserHook<PlaywrightCrawlingContext, ContextExtension>[];
|
|
111
80
|
}
|
|
112
81
|
/**
|
|
113
82
|
* Provides a simple framework for parallel crawling of web pages
|
|
@@ -120,24 +89,26 @@ export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<Playwrig
|
|
|
120
89
|
* If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
|
|
121
90
|
* which downloads the pages using raw HTTP requests and is about 10x faster.
|
|
122
91
|
*
|
|
123
|
-
* The source URLs are represented using {@link Request} objects that are fed from
|
|
124
|
-
* {@link
|
|
125
|
-
*
|
|
92
|
+
* The source URLs are represented using {@link Request} objects that are fed from the
|
|
93
|
+
* {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
|
|
94
|
+
* constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
|
|
95
|
+
* as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
|
|
96
|
+
* {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
|
|
97
|
+
* result as `requestManager`.
|
|
126
98
|
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
|
|
99
|
+
* > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
|
|
100
|
+
* > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
|
|
130
101
|
*
|
|
131
102
|
* The crawler finishes when there are no more {@link Request} objects to crawl.
|
|
132
103
|
*
|
|
133
104
|
* `PlaywrightCrawler` opens a new Chrome page (i.e. tab) for each {@link Request} object to crawl
|
|
134
105
|
* and then calls the function provided by user as the {@link PlaywrightCrawlerOptions.requestHandler} option.
|
|
135
106
|
*
|
|
136
|
-
* New pages are only opened when there is enough free CPU and memory available,
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
* {@link
|
|
107
|
+
* New pages are only opened when there is enough free CPU and memory available, as judged by the crawler's
|
|
108
|
+
* {@link ConcurrencySystem}.
|
|
109
|
+
* Concurrency is tuned via the `minConcurrency`, `maxConcurrency` and `maxRequestsPerMinute` options of the
|
|
110
|
+
* `PlaywrightCrawler` constructor, or, for finer control, by injecting a pre-configured
|
|
111
|
+
* {@link ConcurrencySystem|`concurrencySystem`}.
|
|
141
112
|
*
|
|
142
113
|
* Note that the pool of Playwright instances is internally managed by the [BrowserPool](https://github.com/apify/browser-pool) class.
|
|
143
114
|
*
|
|
@@ -172,16 +143,18 @@ export interface PlaywrightCrawlerOptions extends BrowserCrawlerOptions<Playwrig
|
|
|
172
143
|
* ```
|
|
173
144
|
* @category Crawlers
|
|
174
145
|
*/
|
|
175
|
-
export declare class PlaywrightCrawler extends BrowserCrawler<{
|
|
146
|
+
export declare class PlaywrightCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends PlaywrightCrawlingContext = PlaywrightCrawlingContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<PlaywrightCrawlingContext['request']>>> extends BrowserCrawler<Page, Response, {
|
|
176
147
|
browserPlugins: [PlaywrightPlugin];
|
|
177
|
-
}, LaunchOptions, PlaywrightCrawlingContext> {
|
|
178
|
-
private readonly options;
|
|
179
|
-
readonly config: Configuration;
|
|
148
|
+
}, LaunchOptions, PlaywrightCrawlingContext, ContextExtension, ExtendedContext, Routes> {
|
|
180
149
|
protected static optionsShape: {
|
|
181
150
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
182
151
|
browserPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
183
152
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
184
153
|
launcher: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
154
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
155
|
+
ignoreIframes: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
156
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
157
|
+
ignoreShadowRoots: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
185
158
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
186
159
|
navigationTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
187
160
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -193,17 +166,17 @@ export declare class PlaywrightCrawler extends BrowserCrawler<{
|
|
|
193
166
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
194
167
|
headless: import("ow").AnyPredicate<string | boolean>;
|
|
195
168
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
196
|
-
|
|
169
|
+
browserPool: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
197
170
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
198
|
-
|
|
171
|
+
remoteBrowser: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
199
172
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
200
|
-
|
|
173
|
+
saveResponseCookies: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
201
174
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
202
175
|
proxyConfiguration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
203
176
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
204
|
-
|
|
177
|
+
contextPipelineBuilder: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
205
178
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
206
|
-
|
|
179
|
+
extendContext: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
207
180
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
208
181
|
requestList: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
209
182
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -220,24 +193,42 @@ export declare class PlaywrightCrawler extends BrowserCrawler<{
|
|
|
220
193
|
maxRequestRetries: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
221
194
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
222
195
|
sameDomainDelaySecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
223
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
224
|
-
maxSessionRotations: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
225
196
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
226
197
|
maxRequestsPerCrawl: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
227
198
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
228
|
-
|
|
199
|
+
maxCrawlDepth: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
200
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
201
|
+
taskLoopOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
202
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
203
|
+
concurrencySystem: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
204
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
205
|
+
sessionPool: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
229
206
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
230
207
|
statusMessageLoggingInterval: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
231
208
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
232
209
|
statusMessageCallback: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
210
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
211
|
+
additionalHttpErrorStatusCodes: import("ow").ArrayPredicate<number>;
|
|
212
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
213
|
+
ignoreHttpErrorStatusCodes: import("ow").ArrayPredicate<number>;
|
|
214
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
215
|
+
blockedStatusCodes: import("ow").ArrayPredicate<number>;
|
|
233
216
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
234
217
|
retryOnBlocked: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
235
218
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
236
|
-
respectRobotsTxtFile: import("ow").
|
|
219
|
+
respectRobotsTxtFile: import("ow").AnyPredicate<boolean | object>;
|
|
237
220
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
238
221
|
onSkippedRequest: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
239
222
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
240
223
|
httpClient: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
224
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
225
|
+
configuration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
226
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
227
|
+
storageBackend: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
228
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
229
|
+
eventManager: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
230
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
231
|
+
logger: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
241
232
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
242
233
|
minConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
243
234
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -246,20 +237,50 @@ export declare class PlaywrightCrawler extends BrowserCrawler<{
|
|
|
246
237
|
maxRequestsPerMinute: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
247
238
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
248
239
|
keepAlive: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
249
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
250
|
-
log: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
251
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
252
|
-
experiments: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
253
240
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
254
241
|
statisticsOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
242
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
243
|
+
id: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
255
244
|
};
|
|
256
245
|
/**
|
|
257
246
|
* All `PlaywrightCrawler` parameters are passed via an options object.
|
|
258
247
|
*/
|
|
259
|
-
constructor(options?: PlaywrightCrawlerOptions,
|
|
260
|
-
|
|
248
|
+
constructor(options?: PlaywrightCrawlerOptions<ContextExtension, ExtendedContext, Routes>);
|
|
249
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
250
|
+
protected buildContextPipeline(): import("@crawlee/browser").ContextPipeline<import("@crawlee/browser").CrawlingContext<Dictionary>, BrowserCrawlingContext<Page, Response, Dictionary, Dictionary> & {
|
|
251
|
+
injectFile: (filePath: string, options?: InjectFileOptions) => Promise<unknown>;
|
|
252
|
+
injectJQuery: () => Promise<void>;
|
|
253
|
+
blockRequests: (options?: BlockRequestsOptions) => Promise<void>;
|
|
254
|
+
waitForSelector: (selector: string, timeoutMs?: number) => Promise<void>;
|
|
255
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
256
|
+
parseWithCheerio: (selector?: string, timeoutMs?: number) => Promise<import("@crawlee/browser").CheerioAPI>;
|
|
257
|
+
infiniteScroll: (options?: InfiniteScrollOptions) => Promise<void>;
|
|
258
|
+
listDownloads: () => Promise<Download[]>;
|
|
259
|
+
saveSnapshot: (options?: SaveSnapshotOptions) => Promise<void>;
|
|
260
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
261
|
+
enqueueLinksByClickingElements: (options: Omit<EnqueueLinksByClickingElementsOptions, "page" | "requestManager">) => Promise<import("@crawlee/types").BatchAddRequestsResult>;
|
|
262
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
263
|
+
compileScript: (scriptString: string, ctx?: Dictionary) => import("./utils/playwright-utils.js").CompiledScriptFunction;
|
|
264
|
+
closeCookieModals: () => Promise<void>;
|
|
265
|
+
handleCloudflareChallenge: (options?: HandleCloudflareChallengeOptions) => Promise<Response | undefined>;
|
|
266
|
+
}>;
|
|
261
267
|
protected _navigationHandler(crawlingContext: PlaywrightCrawlingContext, gotoOptions: DirectNavigationOptions): Promise<Response | null>;
|
|
268
|
+
private enhanceContext;
|
|
262
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
|
|
272
|
+
* and propagates the post-challenge {@link Response} back into the crawling context via its return value.
|
|
273
|
+
*
|
|
274
|
+
* **Example usage**
|
|
275
|
+
* ```ts
|
|
276
|
+
* import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
|
|
277
|
+
*
|
|
278
|
+
* const crawler = new PlaywrightCrawler({
|
|
279
|
+
* postNavigationHooks: [handleCloudflareChallengeHook()],
|
|
280
|
+
* });
|
|
281
|
+
* ```
|
|
282
|
+
*/
|
|
283
|
+
export declare function handleCloudflareChallengeHook(options?: HandleCloudflareChallengeOptions): PlaywrightHook;
|
|
263
284
|
/**
|
|
264
285
|
* Creates new {@link Router} instance that works based on request labels.
|
|
265
286
|
* This instance can then serve as a `requestHandler` of your {@link PlaywrightCrawler}.
|
|
@@ -284,6 +305,6 @@ export declare class PlaywrightCrawler extends BrowserCrawler<{
|
|
|
284
305
|
* await crawler.run();
|
|
285
306
|
* ```
|
|
286
307
|
*/
|
|
287
|
-
|
|
288
|
-
export declare function createPlaywrightRouter<Context extends PlaywrightCrawlingContext = PlaywrightCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, UserData
|
|
289
|
-
|
|
308
|
+
export declare function createPlaywrightRouter<Context extends PlaywrightCrawlingContext = PlaywrightCrawlingContext, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<Context['request']>>>(routes?: RouterRoutes<Context, Routes>): RouterHandler<Context, Routes>;
|
|
309
|
+
export declare function createPlaywrightRouter<Context extends PlaywrightCrawlingContext = PlaywrightCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, Record<string, UserData>>): RouterHandler<Context, Record<string, UserData>>;
|
|
310
|
+
export declare function createPlaywrightRouter<Context extends PlaywrightCrawlingContext = PlaywrightCrawlingContext, const Schemas extends RouteSchemas = RouteSchemas>(schemas: Schemas): RouterHandler<Context, RoutesFromSchemas<Schemas>>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BrowserCrawler,
|
|
1
|
+
import { BrowserCrawler, RequestState, Router, serviceLocator } from '@crawlee/browser';
|
|
2
2
|
import ow from 'ow';
|
|
3
3
|
import { PlaywrightLauncher } from './playwright-launcher.js';
|
|
4
|
-
import { gotoExtended,
|
|
4
|
+
import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
|
|
5
5
|
/**
|
|
6
6
|
* Provides a simple framework for parallel crawling of web pages
|
|
7
7
|
* using headless Chromium, Firefox and Webkit browsers with [Playwright](https://github.com/microsoft/playwright).
|
|
@@ -13,24 +13,26 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
13
13
|
* If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
|
|
14
14
|
* which downloads the pages using raw HTTP requests and is about 10x faster.
|
|
15
15
|
*
|
|
16
|
-
* The source URLs are represented using {@link Request} objects that are fed from
|
|
17
|
-
* {@link
|
|
18
|
-
*
|
|
16
|
+
* The source URLs are represented using {@link Request} objects that are fed from the
|
|
17
|
+
* {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
|
|
18
|
+
* constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
|
|
19
|
+
* as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
|
|
20
|
+
* {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
|
|
21
|
+
* result as `requestManager`.
|
|
19
22
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
|
|
23
|
+
* > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
|
|
24
|
+
* > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
|
|
23
25
|
*
|
|
24
26
|
* The crawler finishes when there are no more {@link Request} objects to crawl.
|
|
25
27
|
*
|
|
26
28
|
* `PlaywrightCrawler` opens a new Chrome page (i.e. tab) for each {@link Request} object to crawl
|
|
27
29
|
* and then calls the function provided by user as the {@link PlaywrightCrawlerOptions.requestHandler} option.
|
|
28
30
|
*
|
|
29
|
-
* New pages are only opened when there is enough free CPU and memory available,
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* {@link
|
|
31
|
+
* New pages are only opened when there is enough free CPU and memory available, as judged by the crawler's
|
|
32
|
+
* {@link ConcurrencySystem}.
|
|
33
|
+
* Concurrency is tuned via the `minConcurrency`, `maxConcurrency` and `maxRequestsPerMinute` options of the
|
|
34
|
+
* `PlaywrightCrawler` constructor, or, for finer control, by injecting a pre-configured
|
|
35
|
+
* {@link ConcurrencySystem|`concurrencySystem`}.
|
|
34
36
|
*
|
|
35
37
|
* Note that the pool of Playwright instances is internally managed by the [BrowserPool](https://github.com/apify/browser-pool) class.
|
|
36
38
|
*
|
|
@@ -66,19 +68,19 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
66
68
|
* @category Crawlers
|
|
67
69
|
*/
|
|
68
70
|
export class PlaywrightCrawler extends BrowserCrawler {
|
|
69
|
-
options;
|
|
70
|
-
config;
|
|
71
71
|
static optionsShape = {
|
|
72
72
|
...BrowserCrawler.optionsShape,
|
|
73
73
|
browserPoolOptions: ow.optional.object,
|
|
74
74
|
launcher: ow.optional.object,
|
|
75
|
+
ignoreIframes: ow.optional.boolean,
|
|
76
|
+
ignoreShadowRoots: ow.optional.boolean,
|
|
75
77
|
};
|
|
76
78
|
/**
|
|
77
79
|
* All `PlaywrightCrawler` parameters are passed via an options object.
|
|
78
80
|
*/
|
|
79
|
-
constructor(options = {}
|
|
81
|
+
constructor(options = {}) {
|
|
80
82
|
ow(options, 'PlaywrightCrawlerOptions', ow.object.exactShape(PlaywrightCrawler.optionsShape));
|
|
81
|
-
const { launchContext = {}, headless, ...browserCrawlerOptions } = options;
|
|
83
|
+
const { launchContext = {}, headless, contextPipelineBuilder, ...browserCrawlerOptions } = options;
|
|
82
84
|
const browserPoolOptions = {
|
|
83
85
|
...options.browserPoolOptions,
|
|
84
86
|
};
|
|
@@ -95,45 +97,87 @@ export class PlaywrightCrawler extends BrowserCrawler {
|
|
|
95
97
|
launchContext.launchOptions ??= {};
|
|
96
98
|
launchContext.launchOptions.headless = headless;
|
|
97
99
|
}
|
|
98
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
100
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, options.configuration);
|
|
99
101
|
browserPoolOptions.browserPlugins = [playwrightLauncher.createBrowserPlugin()];
|
|
100
|
-
super({
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
super({
|
|
103
|
+
...browserCrawlerOptions,
|
|
104
|
+
launchContext,
|
|
105
|
+
browserPoolOptions,
|
|
106
|
+
contextPipelineBuilder: contextPipelineBuilder ?? (() => this.buildContextPipeline()),
|
|
107
|
+
});
|
|
103
108
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
await super._runRequestHandler(context);
|
|
109
|
+
buildContextPipeline() {
|
|
110
|
+
return super.buildContextPipeline().compose({ action: this.enhanceContext.bind(this) });
|
|
107
111
|
}
|
|
108
112
|
async _navigationHandler(crawlingContext, gotoOptions) {
|
|
109
113
|
return gotoExtended(crawlingContext.page, crawlingContext.request, gotoOptions);
|
|
110
114
|
}
|
|
115
|
+
async enhanceContext(context) {
|
|
116
|
+
const waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
117
|
+
const locator = context.page.locator(selector).first();
|
|
118
|
+
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
119
|
+
};
|
|
120
|
+
const downloads = [];
|
|
121
|
+
context.page.on('download', (download) => downloads.push(download));
|
|
122
|
+
return {
|
|
123
|
+
injectFile: async (filePath, options) => playwrightUtils.injectFile(context.page, filePath, options),
|
|
124
|
+
injectJQuery: async () => {
|
|
125
|
+
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
126
|
+
context.log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
127
|
+
await playwrightUtils.injectJQuery(context.page);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
await playwrightUtils.injectJQuery(context.page, { surviveNavigations: false });
|
|
131
|
+
},
|
|
132
|
+
blockRequests: async (options) => playwrightUtils.blockRequests(context.page, options),
|
|
133
|
+
waitForSelector,
|
|
134
|
+
parseWithCheerio: async (selector, timeoutMs = 5_000) => {
|
|
135
|
+
if (selector) {
|
|
136
|
+
await waitForSelector(selector, timeoutMs);
|
|
137
|
+
}
|
|
138
|
+
return playwrightUtils.parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
|
|
139
|
+
},
|
|
140
|
+
infiniteScroll: async (options) => playwrightUtils.infiniteScroll(context.page, options),
|
|
141
|
+
listDownloads: async () => downloads,
|
|
142
|
+
saveSnapshot: async (options) => playwrightUtils.saveSnapshot(context.page, {
|
|
143
|
+
...options,
|
|
144
|
+
configuration: serviceLocator.getConfiguration(),
|
|
145
|
+
}),
|
|
146
|
+
enqueueLinksByClickingElements: async (options) => playwrightUtils.enqueueLinksByClickingElements({
|
|
147
|
+
...options,
|
|
148
|
+
page: context.page,
|
|
149
|
+
requestManager: this.requestManager,
|
|
150
|
+
}),
|
|
151
|
+
compileScript: (scriptString, ctx) => playwrightUtils.compileScript(scriptString, ctx),
|
|
152
|
+
closeCookieModals: async () => playwrightUtils.closeCookieModals(context.page),
|
|
153
|
+
handleCloudflareChallenge: async (options) => {
|
|
154
|
+
return playwrightUtils.handleCloudflareChallenge(context.page, context.request.url, options);
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
}
|
|
111
158
|
}
|
|
112
159
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
* Defaults to the {@link PlaywrightCrawlingContext}.
|
|
116
|
-
*
|
|
117
|
-
* > Serves as a shortcut for using `Router.create<PlaywrightCrawlingContext>()`.
|
|
160
|
+
* Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
|
|
161
|
+
* and propagates the post-challenge {@link Response} back into the crawling context via its return value.
|
|
118
162
|
*
|
|
163
|
+
* **Example usage**
|
|
119
164
|
* ```ts
|
|
120
|
-
* import { PlaywrightCrawler,
|
|
121
|
-
*
|
|
122
|
-
* const router = createPlaywrightRouter();
|
|
123
|
-
* router.addHandler('label-a', async (ctx) => {
|
|
124
|
-
* ctx.log.info('...');
|
|
125
|
-
* });
|
|
126
|
-
* router.addDefaultHandler(async (ctx) => {
|
|
127
|
-
* ctx.log.info('...');
|
|
128
|
-
* });
|
|
165
|
+
* import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
|
|
129
166
|
*
|
|
130
167
|
* const crawler = new PlaywrightCrawler({
|
|
131
|
-
*
|
|
168
|
+
* postNavigationHooks: [handleCloudflareChallengeHook()],
|
|
132
169
|
* });
|
|
133
|
-
* await crawler.run();
|
|
134
170
|
* ```
|
|
135
171
|
*/
|
|
136
|
-
export function
|
|
137
|
-
return
|
|
172
|
+
export function handleCloudflareChallengeHook(options) {
|
|
173
|
+
return async (context) => {
|
|
174
|
+
const response = await context.handleCloudflareChallenge(options);
|
|
175
|
+
if (response !== undefined) {
|
|
176
|
+
return { response };
|
|
177
|
+
}
|
|
178
|
+
return undefined;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
export function createPlaywrightRouter(routesOrSchemas) {
|
|
182
|
+
return Router.create(routesOrSchemas);
|
|
138
183
|
}
|
|
139
|
-
//# sourceMappingURL=playwright-crawler.js.map
|
|
@@ -70,7 +70,7 @@ export interface PlaywrightLaunchContext extends BrowserLaunchContext<LaunchOpti
|
|
|
70
70
|
* @ignore
|
|
71
71
|
*/
|
|
72
72
|
export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin> {
|
|
73
|
-
readonly
|
|
73
|
+
readonly configuration: Configuration;
|
|
74
74
|
protected static optionsShape: {
|
|
75
75
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
76
76
|
launcher: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
@@ -84,6 +84,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
84
84
|
useIncognitoPages: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
85
85
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
86
86
|
browserPerProxy: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
|
+
ignoreProxyCertificate: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
89
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
90
|
userDataDir: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
89
91
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -94,7 +96,7 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
94
96
|
/**
|
|
95
97
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
96
98
|
*/
|
|
97
|
-
constructor(launchContext?: PlaywrightLaunchContext,
|
|
99
|
+
constructor(launchContext?: PlaywrightLaunchContext, configuration?: Configuration);
|
|
98
100
|
}
|
|
99
101
|
/**
|
|
100
102
|
* Launches headless browsers using Playwright pre-configured to work within the Apify platform.
|
|
@@ -125,9 +127,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
125
127
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
126
128
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
127
129
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
128
|
-
* @param [
|
|
130
|
+
* @param [configuration]
|
|
129
131
|
* @returns
|
|
130
132
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
131
133
|
*/
|
|
132
|
-
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext,
|
|
133
|
-
//# sourceMappingURL=playwright-launcher.d.ts.map
|
|
134
|
+
export declare function launchPlaywright(launchContext?: PlaywrightLaunchContext, configuration?: Configuration): Promise<Browser>;
|
|
@@ -6,7 +6,7 @@ import ow from 'ow';
|
|
|
6
6
|
* @ignore
|
|
7
7
|
*/
|
|
8
8
|
export class PlaywrightLauncher extends BrowserLauncher {
|
|
9
|
-
|
|
9
|
+
configuration;
|
|
10
10
|
static optionsShape = {
|
|
11
11
|
...BrowserLauncher.optionsShape,
|
|
12
12
|
launcher: ow.optional.object,
|
|
@@ -15,7 +15,7 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
15
15
|
/**
|
|
16
16
|
* All `PlaywrightLauncher` parameters are passed via this launchContext object.
|
|
17
17
|
*/
|
|
18
|
-
constructor(launchContext = {},
|
|
18
|
+
constructor(launchContext = {}, configuration = Configuration.getGlobalConfiguration()) {
|
|
19
19
|
ow(launchContext, 'PlaywrightLauncherOptions', ow.object.exactShape(PlaywrightLauncher.optionsShape));
|
|
20
20
|
const { launcher = BrowserLauncher.requireLauncherOrThrow('playwright', 'apify/actor-node-playwright-*').chromium, } = launchContext;
|
|
21
21
|
const { launchOptions = {}, ...rest } = launchContext;
|
|
@@ -23,11 +23,11 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
23
23
|
...rest,
|
|
24
24
|
launchOptions: {
|
|
25
25
|
...launchOptions,
|
|
26
|
-
executablePath: getDefaultExecutablePath(launchContext,
|
|
26
|
+
executablePath: getDefaultExecutablePath(launchContext, configuration),
|
|
27
27
|
},
|
|
28
28
|
launcher,
|
|
29
|
-
},
|
|
30
|
-
this.
|
|
29
|
+
}, configuration);
|
|
30
|
+
this.configuration = configuration;
|
|
31
31
|
this.Plugin = PlaywrightPlugin;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -36,8 +36,8 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
36
36
|
* @returns default path to browser.
|
|
37
37
|
* @ignore
|
|
38
38
|
*/
|
|
39
|
-
function getDefaultExecutablePath(launchContext,
|
|
40
|
-
const pathFromPlaywrightImage =
|
|
39
|
+
function getDefaultExecutablePath(launchContext, configuration) {
|
|
40
|
+
const pathFromPlaywrightImage = configuration.defaultBrowserPath;
|
|
41
41
|
const { launchOptions = {} } = launchContext;
|
|
42
42
|
if (launchOptions.executablePath) {
|
|
43
43
|
return launchOptions.executablePath;
|
|
@@ -79,12 +79,11 @@ function getDefaultExecutablePath(launchContext, config) {
|
|
|
79
79
|
* Optional settings passed to `browserType.launch()`. In addition to
|
|
80
80
|
* [Playwright's options](https://playwright.dev/docs/api/class-browsertype?_highlight=launch#browsertypelaunchoptions)
|
|
81
81
|
* the object may contain our own {@link PlaywrightLaunchContext} that enable additional features.
|
|
82
|
-
* @param [
|
|
82
|
+
* @param [configuration]
|
|
83
83
|
* @returns
|
|
84
84
|
* Promise that resolves to Playwright's `Browser` instance.
|
|
85
85
|
*/
|
|
86
|
-
export async function launchPlaywright(launchContext,
|
|
87
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
86
|
+
export async function launchPlaywright(launchContext, configuration = Configuration.getGlobalConfiguration()) {
|
|
87
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, configuration);
|
|
88
88
|
return playwrightLauncher.launch();
|
|
89
89
|
}
|
|
90
|
-
//# sourceMappingURL=playwright-launcher.js.map
|