@crawlee/playwright 4.0.0-beta.3 → 4.0.0-beta.300
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 +18 -14
- package/index.d.ts +4 -3
- package/index.js +2 -2
- package/internals/adaptive-playwright-crawler.d.ts +149 -94
- package/internals/adaptive-playwright-crawler.js +561 -274
- package/internals/enqueue-links/click-elements.d.ts +37 -55
- package/internals/enqueue-links/click-elements.js +65 -55
- package/internals/playwright-browser-pool.d.ts +55 -0
- package/internals/playwright-browser-pool.js +34 -0
- package/internals/playwright-crawler.d.ts +184 -172
- package/internals/playwright-crawler.js +125 -60
- package/internals/playwright-launcher.d.ts +30 -20
- package/internals/playwright-launcher.js +24 -18
- package/internals/utils/playwright-utils.d.ts +74 -48
- package/internals/utils/playwright-utils.js +159 -158
- package/internals/utils/rendering-type-prediction.d.ts +41 -16
- package/internals/utils/rendering-type-prediction.js +101 -37
- package/package.json +19 -18
- 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
|
@@ -20,20 +20,47 @@
|
|
|
20
20
|
import { readFile } from 'node:fs/promises';
|
|
21
21
|
import { createRequire } from 'node:module';
|
|
22
22
|
import vm from 'node:vm';
|
|
23
|
-
import { Configuration, KeyValueStore,
|
|
24
|
-
import {
|
|
25
|
-
import
|
|
26
|
-
import {
|
|
27
|
-
import ow from 'ow';
|
|
23
|
+
import { Configuration, KeyValueStore, serviceLocator, SessionError, validators } from '@crawlee/browser';
|
|
24
|
+
import { sleep } from '@crawlee/utils';
|
|
25
|
+
import { expandShadowRoots, parseArgument, schemas } from '@crawlee/utils/internal';
|
|
26
|
+
import { z } from 'zod';
|
|
28
27
|
import { LruCache } from '@apify/datastructures';
|
|
29
|
-
import log_ from '@apify/log';
|
|
30
28
|
import { enqueueLinksByClickingElements } from '../enqueue-links/click-elements.js';
|
|
31
|
-
|
|
32
|
-
const log = log_.child({ prefix: 'Playwright Utils' });
|
|
29
|
+
const getLog = () => serviceLocator.getChildLog('Playwright Utils');
|
|
33
30
|
const require = createRequire(import.meta.url);
|
|
34
31
|
const jqueryPath = require.resolve('jquery');
|
|
35
32
|
const MAX_INJECT_FILE_CACHE_SIZE = 10;
|
|
36
33
|
const DEFAULT_BLOCK_REQUEST_URL_PATTERNS = ['.css', '.jpg', '.jpeg', '.png', '.svg', '.gif', '.woff', '.pdf', '.zip'];
|
|
34
|
+
const filePathSchema = z.string();
|
|
35
|
+
const injectFileOptionsSchema = z.strictObject({
|
|
36
|
+
surviveNavigations: z.boolean().optional(),
|
|
37
|
+
});
|
|
38
|
+
const gotoExtendedRequestSchema = z.looseObject({
|
|
39
|
+
url: z.url(),
|
|
40
|
+
method: z.string().optional(),
|
|
41
|
+
headers: schemas.anyObject.optional(),
|
|
42
|
+
payload: z.union([z.string(), z.instanceof(Uint8Array)]).optional(),
|
|
43
|
+
});
|
|
44
|
+
const blockRequestsOptionsSchema = z.strictObject({
|
|
45
|
+
urlPatterns: schemas.arrayOf(z.string(), 'strings').default(DEFAULT_BLOCK_REQUEST_URL_PATTERNS),
|
|
46
|
+
extraUrlPatterns: schemas.arrayOf(z.string(), 'strings').default(() => []),
|
|
47
|
+
});
|
|
48
|
+
const infiniteScrollOptionsSchema = z.strictObject({
|
|
49
|
+
timeoutSecs: schemas.anyNumber.default(0),
|
|
50
|
+
maxScrollHeight: schemas.anyNumber.default(0),
|
|
51
|
+
waitForSecs: schemas.anyNumber.default(4),
|
|
52
|
+
scrollDownAndUp: z.boolean().default(false),
|
|
53
|
+
buttonSelector: z.string().optional(),
|
|
54
|
+
stopScrollCallback: schemas.anyFunction.optional(),
|
|
55
|
+
});
|
|
56
|
+
const saveSnapshotOptionsSchema = z.strictObject({
|
|
57
|
+
key: z.string().min(1).default('SNAPSHOT'),
|
|
58
|
+
screenshotQuality: schemas.anyNumber.default(50),
|
|
59
|
+
saveScreenshot: z.boolean().default(true),
|
|
60
|
+
saveHtml: z.boolean().default(true),
|
|
61
|
+
keyValueStoreName: z.string().optional(),
|
|
62
|
+
configuration: schemas.anyObject.optional(),
|
|
63
|
+
});
|
|
37
64
|
/**
|
|
38
65
|
* Cache contents of previously injected files to limit file system access.
|
|
39
66
|
*/
|
|
@@ -50,21 +77,19 @@ const injectedFilesCache = new LruCache({ maxLength: MAX_INJECT_FILE_CACHE_SIZE
|
|
|
50
77
|
* @param [options]
|
|
51
78
|
*/
|
|
52
79
|
export async function injectFile(page, filePath, options = {}) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
surviveNavigations: ow.optional.boolean,
|
|
57
|
-
}));
|
|
80
|
+
parseArgument(page, validators.browserPage);
|
|
81
|
+
parseArgument(filePath, filePathSchema);
|
|
82
|
+
const { surviveNavigations } = parseArgument(options, injectFileOptionsSchema);
|
|
58
83
|
let contents = injectedFilesCache.get(filePath);
|
|
59
84
|
if (!contents) {
|
|
60
85
|
contents = await readFile(filePath, 'utf8');
|
|
61
86
|
injectedFilesCache.add(filePath, contents);
|
|
62
87
|
}
|
|
63
88
|
const evalP = page.evaluate(contents);
|
|
64
|
-
if (
|
|
89
|
+
if (surviveNavigations) {
|
|
65
90
|
page.on('framenavigated', async () => page
|
|
66
91
|
.evaluate(contents)
|
|
67
|
-
.catch((error) =>
|
|
92
|
+
.catch((error) => getLog().warning('An error occurred during the script injection!', { error })));
|
|
68
93
|
}
|
|
69
94
|
return evalP;
|
|
70
95
|
}
|
|
@@ -95,7 +120,7 @@ export async function injectFile(page, filePath, options = {}) {
|
|
|
95
120
|
* @param [options.surviveNavigations] Opt-out option to disable the JQuery reinjection after navigation.
|
|
96
121
|
*/
|
|
97
122
|
export async function injectJQuery(page, options) {
|
|
98
|
-
|
|
123
|
+
parseArgument(page, validators.browserPage);
|
|
99
124
|
return injectFile(page, jqueryPath, { surviveNavigations: options?.surviveNavigations ?? true });
|
|
100
125
|
}
|
|
101
126
|
/**
|
|
@@ -111,19 +136,14 @@ export async function injectJQuery(page, options) {
|
|
|
111
136
|
* @param [gotoOptions] Custom options for `page.goto()`.
|
|
112
137
|
*/
|
|
113
138
|
export async function gotoExtended(page, request, gotoOptions = {}) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
method: ow.optional.string,
|
|
118
|
-
headers: ow.optional.object,
|
|
119
|
-
payload: ow.optional.any(ow.string, ow.uint8Array),
|
|
120
|
-
}));
|
|
121
|
-
ow(gotoOptions, ow.object);
|
|
139
|
+
parseArgument(page, validators.browserPage);
|
|
140
|
+
parseArgument(request, gotoExtendedRequestSchema);
|
|
141
|
+
parseArgument(gotoOptions, schemas.anyObject);
|
|
122
142
|
const { url, method, headers, payload } = request;
|
|
123
143
|
const isEmpty = (o) => !o || Object.keys(o).length === 0;
|
|
124
|
-
if (method !== 'GET' || payload
|
|
144
|
+
if (method !== 'GET' || payload) {
|
|
125
145
|
// This is not deprecated, we use it to log only once.
|
|
126
|
-
|
|
146
|
+
getLog().deprecated('Using other request methods than GET, rewriting headers and adding payloads has a high impact on performance ' +
|
|
127
147
|
'in recent versions of Playwright. Use only when necessary.');
|
|
128
148
|
let wasCalled = false;
|
|
129
149
|
const interceptRequestHandler = async (route) => {
|
|
@@ -144,12 +164,15 @@ export async function gotoExtended(page, request, gotoOptions = {}) {
|
|
|
144
164
|
await route.continue(overrides);
|
|
145
165
|
}
|
|
146
166
|
catch (error) {
|
|
147
|
-
|
|
167
|
+
getLog().debug('Error inside request interceptor', { error });
|
|
148
168
|
}
|
|
149
169
|
return undefined;
|
|
150
170
|
};
|
|
151
171
|
await page.route('**/*', interceptRequestHandler);
|
|
152
172
|
}
|
|
173
|
+
else if (!isEmpty(headers)) {
|
|
174
|
+
await page.setExtraHTTPHeaders(headers);
|
|
175
|
+
}
|
|
153
176
|
return page.goto(url, gotoOptions);
|
|
154
177
|
}
|
|
155
178
|
/**
|
|
@@ -200,12 +223,8 @@ export async function gotoExtended(page, request, gotoOptions = {}) {
|
|
|
200
223
|
* @param [options]
|
|
201
224
|
*/
|
|
202
225
|
export async function blockRequests(page, options = {}) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
urlPatterns: ow.optional.array.ofType(ow.string),
|
|
206
|
-
extraUrlPatterns: ow.optional.array.ofType(ow.string),
|
|
207
|
-
}));
|
|
208
|
-
const { urlPatterns = DEFAULT_BLOCK_REQUEST_URL_PATTERNS, extraUrlPatterns = [] } = options;
|
|
226
|
+
parseArgument(page, validators.browserPage);
|
|
227
|
+
const { urlPatterns, extraUrlPatterns } = parseArgument(options, blockRequestsOptionsSchema);
|
|
209
228
|
const patternsToBlock = [...urlPatterns, ...extraUrlPatterns];
|
|
210
229
|
try {
|
|
211
230
|
const client = await page.context().newCDPSession(page);
|
|
@@ -213,7 +232,7 @@ export async function blockRequests(page, options = {}) {
|
|
|
213
232
|
await client.send('Network.setBlockedURLs', { urls: patternsToBlock });
|
|
214
233
|
}
|
|
215
234
|
catch {
|
|
216
|
-
|
|
235
|
+
getLog().warning('blockRequests() helper is incompatible with non-Chromium browsers.');
|
|
217
236
|
}
|
|
218
237
|
}
|
|
219
238
|
/**
|
|
@@ -249,7 +268,7 @@ export function compileScript(scriptString, context = Object.create(null)) {
|
|
|
249
268
|
func = vm.runInNewContext(funcString, context); // "Secure" the context by removing prototypes, unless custom context is provided.
|
|
250
269
|
}
|
|
251
270
|
catch (err) {
|
|
252
|
-
|
|
271
|
+
getLog().exception(err, 'Cannot compile script!');
|
|
253
272
|
throw err;
|
|
254
273
|
}
|
|
255
274
|
if (typeof func !== 'function')
|
|
@@ -263,16 +282,8 @@ export function compileScript(scriptString, context = Object.create(null)) {
|
|
|
263
282
|
* @param [options]
|
|
264
283
|
*/
|
|
265
284
|
export async function infiniteScroll(page, options = {}) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
timeoutSecs: ow.optional.number,
|
|
269
|
-
maxScrollHeight: ow.optional.number,
|
|
270
|
-
waitForSecs: ow.optional.number,
|
|
271
|
-
scrollDownAndUp: ow.optional.boolean,
|
|
272
|
-
buttonSelector: ow.optional.string,
|
|
273
|
-
stopScrollCallback: ow.optional.function,
|
|
274
|
-
}));
|
|
275
|
-
const { timeoutSecs = 0, maxScrollHeight = 0, waitForSecs = 4, scrollDownAndUp = false, buttonSelector, stopScrollCallback, } = options;
|
|
285
|
+
parseArgument(page, validators.browserPage);
|
|
286
|
+
const { timeoutSecs, maxScrollHeight, waitForSecs, scrollDownAndUp, buttonSelector, stopScrollCallback } = parseArgument(options, infiniteScrollOptionsSchema);
|
|
276
287
|
let finished;
|
|
277
288
|
const startTime = Date.now();
|
|
278
289
|
const CHECK_INTERVAL_MILLIS = 1000;
|
|
@@ -349,19 +360,11 @@ export async function infiniteScroll(page, options = {}) {
|
|
|
349
360
|
* @param [options]
|
|
350
361
|
*/
|
|
351
362
|
export async function saveSnapshot(page, options = {}) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
key: ow.optional.string.nonEmpty,
|
|
355
|
-
screenshotQuality: ow.optional.number,
|
|
356
|
-
saveScreenshot: ow.optional.boolean,
|
|
357
|
-
saveHtml: ow.optional.boolean,
|
|
358
|
-
keyValueStoreName: ow.optional.string,
|
|
359
|
-
config: ow.optional.object,
|
|
360
|
-
}));
|
|
361
|
-
const { key = 'SNAPSHOT', screenshotQuality = 50, saveScreenshot = true, saveHtml = true, keyValueStoreName, config, } = options;
|
|
363
|
+
parseArgument(page, validators.browserPage);
|
|
364
|
+
const { key, screenshotQuality, saveScreenshot, saveHtml, keyValueStoreName, configuration } = parseArgument(options, saveSnapshotOptionsSchema);
|
|
362
365
|
try {
|
|
363
|
-
const store = await KeyValueStore.open(keyValueStoreName, {
|
|
364
|
-
|
|
366
|
+
const store = await KeyValueStore.open(keyValueStoreName ? { name: keyValueStoreName } : null, {
|
|
367
|
+
configuration: configuration ?? Configuration.getGlobalConfiguration(),
|
|
365
368
|
});
|
|
366
369
|
if (saveScreenshot) {
|
|
367
370
|
const screenshotName = `${key}.jpg`;
|
|
@@ -396,36 +399,44 @@ export async function saveSnapshot(page, options = {}) {
|
|
|
396
399
|
* @param ignoreShadowRoots
|
|
397
400
|
*/
|
|
398
401
|
export async function parseWithCheerio(page, ignoreShadowRoots = false, ignoreIframes = false) {
|
|
399
|
-
|
|
402
|
+
parseArgument(page, validators.browserPage);
|
|
403
|
+
const html = ignoreShadowRoots
|
|
404
|
+
? null
|
|
405
|
+
: (await page.evaluate(`(${expandShadowRoots.toString()})(document)`));
|
|
406
|
+
const pageContent = html || (await page.content());
|
|
407
|
+
// Full cheerio (parse5) import for browser compliant parser
|
|
408
|
+
const { load } = await import('cheerio');
|
|
409
|
+
const $ = load(pageContent);
|
|
400
410
|
if (page.frames().length > 1 && !ignoreIframes) {
|
|
401
411
|
const frames = await page.$$('iframe');
|
|
402
|
-
|
|
412
|
+
const cheerioIframes = $('iframe').toArray();
|
|
413
|
+
if (frames.length !== cheerioIframes.length) {
|
|
414
|
+
serviceLocator
|
|
415
|
+
.getLogger()
|
|
416
|
+
.warning(`parseWithCheerio: iframe count mismatch between live DOM (${frames.length}) and page snapshot (${cheerioIframes.length}). Some iframes may not be expanded.`);
|
|
417
|
+
}
|
|
418
|
+
await Promise.all(frames.map(async (frame, index) => {
|
|
403
419
|
try {
|
|
404
420
|
const iframe = await frame.contentFrame();
|
|
405
|
-
if (iframe) {
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
421
|
+
if (iframe && cheerioIframes[index]) {
|
|
422
|
+
const getIframeHTML = async () => {
|
|
423
|
+
try {
|
|
424
|
+
return iframe.locator('body').first().innerHTML();
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
return iframe.content();
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
const contents = await getIframeHTML();
|
|
431
|
+
$(cheerioIframes[index]).replaceWith(`<div class="crawlee-iframe-replacement">${contents}</div>`);
|
|
413
432
|
}
|
|
414
433
|
}
|
|
415
434
|
catch (error) {
|
|
416
|
-
|
|
435
|
+
getLog().warning(`Failed to extract iframe content: ${error}`);
|
|
417
436
|
}
|
|
418
437
|
}));
|
|
419
438
|
}
|
|
420
|
-
|
|
421
|
-
? null
|
|
422
|
-
: (await page.evaluate(`(${expandShadowRoots.toString()})(document)`));
|
|
423
|
-
const pageContent = html || (await page.content());
|
|
424
|
-
return cheerio.load(pageContent);
|
|
425
|
-
}
|
|
426
|
-
export async function closeCookieModals(page) {
|
|
427
|
-
ow(page, ow.object.validate(validators.browserPage));
|
|
428
|
-
await page.evaluate(getCookieClosingScript());
|
|
439
|
+
return $;
|
|
429
440
|
}
|
|
430
441
|
/**
|
|
431
442
|
* This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
|
|
@@ -434,78 +445,112 @@ export async function closeCookieModals(page) {
|
|
|
434
445
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
435
446
|
* into the `requestHandler`.
|
|
436
447
|
*
|
|
448
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned, so
|
|
449
|
+
* it can be propagated back to the crawling context via a hook return value (see
|
|
450
|
+
* {@link handleCloudflareChallengeHook}). Thanks to that, the 403 status of the challenge page does not
|
|
451
|
+
* need to be removed from `blockedStatusCodes` - a solved challenge replaces the response before the
|
|
452
|
+
* blocked-status check runs, and an unsolved one is retried with a fresh session.
|
|
453
|
+
*
|
|
437
454
|
* Works best with camoufox.
|
|
438
455
|
*
|
|
439
456
|
* **Example usage**
|
|
440
457
|
* ```ts
|
|
441
458
|
* postNavigationHooks: [
|
|
442
|
-
* async (
|
|
443
|
-
* await handleCloudflareChallenge();
|
|
459
|
+
* async (context) => {
|
|
460
|
+
* const response = await context.handleCloudflareChallenge();
|
|
461
|
+
* // `{ response: undefined }` would replace the navigation response and skip the blocked-status check
|
|
462
|
+
* return response && { response };
|
|
444
463
|
* },
|
|
445
464
|
* ],
|
|
446
465
|
* ```
|
|
447
466
|
*
|
|
448
467
|
* @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object
|
|
449
468
|
* @param url current URL for request identification, only used for logging
|
|
450
|
-
* @param [session] current session object
|
|
451
469
|
* @param [options]
|
|
470
|
+
* @param [response] navigation response, used to poll for late-rendered challenge markup when it signals a likely challenge
|
|
452
471
|
*/
|
|
453
|
-
async function handleCloudflareChallenge(page, url,
|
|
454
|
-
//
|
|
455
|
-
|
|
456
|
-
//
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
return document.querySelector('h1')?.textContent?.trim().includes('Sorry, you have been blocked');
|
|
472
|
+
export async function handleCloudflareChallenge(page, url, options = {}, response) {
|
|
473
|
+
// The options object may outlive this call (the pre-wrapped hook reuses one instance for all
|
|
474
|
+
// requests and retries), so the defaults are kept local instead of being memoized on `options` -
|
|
475
|
+
// memoizing them would pin the closed-over `page` of the first call forever.
|
|
476
|
+
const isBlockedCallback = options.isBlockedCallback ??
|
|
477
|
+
(async (p) => {
|
|
478
|
+
const isBlocked = await p.evaluate(() => {
|
|
479
|
+
return document.querySelector('h1')?.textContent?.trim().includes('Sorry, you have been blocked');
|
|
480
|
+
});
|
|
481
|
+
return !!isBlocked;
|
|
464
482
|
});
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
483
|
+
const isChallengeCallback = options.isChallengeCallback ??
|
|
484
|
+
(async (p) => {
|
|
485
|
+
return await p.evaluate(async () => {
|
|
486
|
+
// Cloudflare keeps reshuffling the wrapper elements between `.footer-inner` and `.ray-id`,
|
|
487
|
+
// so only the stable outer classes are matched.
|
|
488
|
+
return !!document.querySelector('.footer .footer-inner .ray-id');
|
|
489
|
+
});
|
|
470
490
|
});
|
|
471
|
-
};
|
|
472
491
|
const retryBlocked = async () => {
|
|
473
|
-
const isBlocked = await
|
|
492
|
+
const isBlocked = await isBlockedCallback(page).catch(() => false);
|
|
474
493
|
if (isBlocked) {
|
|
475
494
|
throw new SessionError(`Blocked by Cloudflare when processing ${url}`);
|
|
476
495
|
}
|
|
477
496
|
};
|
|
478
497
|
// check if we ended up on the CF challenge page
|
|
479
498
|
const isChallenge = async () => {
|
|
480
|
-
return
|
|
499
|
+
return isChallengeCallback(page).catch(async () => {
|
|
500
|
+
// the challenge page navigates itself, which destroys the context of an in-flight check
|
|
501
|
+
await page.waitForLoadState('domcontentloaded').catch(() => { });
|
|
502
|
+
return isChallengeCallback(page).catch(() => false);
|
|
503
|
+
});
|
|
481
504
|
};
|
|
482
|
-
|
|
505
|
+
// The challenge markup can finish rendering only after the `load` event, so when the response signals
|
|
506
|
+
// a likely challenge, poll for it instead of checking once. A 403 alone is not enough - it must come
|
|
507
|
+
// from Cloudflare (the `cf-ray` header), so 403s served by other stacks keep failing fast.
|
|
508
|
+
const headers = response?.headers() ?? {};
|
|
509
|
+
const likelyChallenge = headers['cf-mitigated'] === 'challenge' || (response?.status() === 403 && 'cf-ray' in headers);
|
|
510
|
+
let detected = await isChallenge();
|
|
511
|
+
for (let i = 0; !detected && likelyChallenge && i < 10; i++) {
|
|
512
|
+
// a hard-blocked page never turns into a challenge, so fail fast instead of finishing the poll
|
|
513
|
+
await retryBlocked();
|
|
514
|
+
await sleep(500);
|
|
515
|
+
detected = await isChallenge();
|
|
516
|
+
}
|
|
517
|
+
if (!detected) {
|
|
483
518
|
await retryBlocked();
|
|
484
|
-
return;
|
|
519
|
+
return undefined;
|
|
485
520
|
}
|
|
486
521
|
const logLevel = options.verbose ? 'info' : 'debug';
|
|
487
|
-
|
|
522
|
+
getLog()[logLevel](`Detected Cloudflare challenge at ${url}, trying to solve it. This can take up to ${10 + (options.sleepSecs ?? 10)} seconds.`);
|
|
488
523
|
const bb = await page
|
|
489
524
|
.evaluate(() => {
|
|
490
|
-
|
|
525
|
+
// Prefer the actual challenge widget (the box holding the Turnstile checkbox input);
|
|
526
|
+
// fall back to the first content div for older challenge layouts.
|
|
527
|
+
const div = document.querySelector('.main-content div:has(input[id^="cf-chl-widget-"])') ??
|
|
528
|
+
document.querySelector('.main-content div');
|
|
491
529
|
return div?.getBoundingClientRect();
|
|
492
530
|
})
|
|
493
531
|
.catch(() => undefined);
|
|
494
532
|
if (!bb) {
|
|
495
|
-
return;
|
|
533
|
+
return undefined;
|
|
496
534
|
}
|
|
497
535
|
const randomOffset = (range) => {
|
|
498
536
|
return Math.round(100 * range * Math.random()) / 100;
|
|
499
537
|
};
|
|
500
|
-
|
|
501
|
-
|
|
538
|
+
let x = bb.x + 30;
|
|
539
|
+
let y = bb.y + 25;
|
|
502
540
|
// try to click the checkbox every second
|
|
503
541
|
for (let i = 0; i < 10; i++) {
|
|
504
|
-
await sleep(1000);
|
|
542
|
+
await sleep((options.preChallengeSleepSecs ?? 1) * 1000);
|
|
505
543
|
// break early if we are no longer on the CF challenge page
|
|
506
544
|
if (!(await isChallenge())) {
|
|
507
545
|
break;
|
|
508
546
|
}
|
|
547
|
+
if (options.clickPositionCallback) {
|
|
548
|
+
const pos = await options.clickPositionCallback(page);
|
|
549
|
+
if (pos) {
|
|
550
|
+
x = pos.x;
|
|
551
|
+
y = pos.y;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
509
554
|
if (options.clickCallback) {
|
|
510
555
|
await options.clickCallback(page, { x, y });
|
|
511
556
|
continue;
|
|
@@ -513,7 +558,10 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
513
558
|
// we can click on the text too, so X can be a bit larger
|
|
514
559
|
const xRandomized = x + randomOffset(10);
|
|
515
560
|
const yRandomized = y + randomOffset(10);
|
|
516
|
-
|
|
561
|
+
getLog()[logLevel](`Trying to click on the Cloudflare checkbox at ${url}`, {
|
|
562
|
+
x: xRandomized,
|
|
563
|
+
y: yRandomized,
|
|
564
|
+
});
|
|
517
565
|
await page.mouse.click(xRandomized, yRandomized);
|
|
518
566
|
// sometimes the checkbox is lower (could be caused by a lag when rendering the logo)
|
|
519
567
|
await page.mouse.click(xRandomized, yRandomized + 35);
|
|
@@ -523,55 +571,8 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
523
571
|
throw new SessionError(`Blocked by Cloudflare when processing ${url}`);
|
|
524
572
|
}
|
|
525
573
|
await retryBlocked();
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
context.injectJQuery = async () => {
|
|
530
|
-
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
531
|
-
log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
532
|
-
await injectJQuery(context.page);
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
await injectJQuery(context.page, { surviveNavigations: false });
|
|
536
|
-
};
|
|
537
|
-
context.blockRequests = async (options) => blockRequests(context.page, options);
|
|
538
|
-
context.waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
539
|
-
const locator = context.page.locator(selector).first();
|
|
540
|
-
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
541
|
-
};
|
|
542
|
-
context.parseWithCheerio = async (selector, timeoutMs = 5_000) => {
|
|
543
|
-
if (selector) {
|
|
544
|
-
await context.waitForSelector(selector, timeoutMs);
|
|
545
|
-
}
|
|
546
|
-
return parseWithCheerio(context.page, crawlerOptions.ignoreShadowRoots, crawlerOptions.ignoreIframes);
|
|
547
|
-
};
|
|
548
|
-
context.infiniteScroll = async (options) => infiniteScroll(context.page, options);
|
|
549
|
-
context.saveSnapshot = async (options) => saveSnapshot(context.page, { ...options, config: context.crawler.config });
|
|
550
|
-
context.enqueueLinksByClickingElements = async (options) => enqueueLinksByClickingElements({
|
|
551
|
-
...options,
|
|
552
|
-
page: context.page,
|
|
553
|
-
requestQueue: context.crawler.requestQueue,
|
|
554
|
-
});
|
|
555
|
-
context.compileScript = (scriptString, ctx) => compileScript(scriptString, ctx);
|
|
556
|
-
context.closeCookieModals = async () => closeCookieModals(context.page);
|
|
557
|
-
context.handleCloudflareChallenge = async (options) => {
|
|
558
|
-
return handleCloudflareChallenge(context.page, context.request.url, context.session, options);
|
|
559
|
-
};
|
|
574
|
+
// Reload to obtain a fresh Response without the challenge interstitial, which the caller can
|
|
575
|
+
// propagate back into the crawling context so downstream status-code checks see the new value.
|
|
576
|
+
return (await page.reload()) ?? undefined;
|
|
560
577
|
}
|
|
561
578
|
export { enqueueLinksByClickingElements };
|
|
562
|
-
/** @internal */
|
|
563
|
-
export const playwrightUtils = {
|
|
564
|
-
injectFile,
|
|
565
|
-
injectJQuery,
|
|
566
|
-
gotoExtended,
|
|
567
|
-
blockRequests,
|
|
568
|
-
enqueueLinksByClickingElements,
|
|
569
|
-
parseWithCheerio,
|
|
570
|
-
infiniteScroll,
|
|
571
|
-
saveSnapshot,
|
|
572
|
-
compileScript,
|
|
573
|
-
closeCookieModals,
|
|
574
|
-
RenderingTypePredictor,
|
|
575
|
-
handleCloudflareChallenge,
|
|
576
|
-
};
|
|
577
|
-
//# sourceMappingURL=playwright-utils.js.map
|
|
@@ -1,21 +1,51 @@
|
|
|
1
|
-
import type { Request } from '@crawlee/core';
|
|
1
|
+
import type { RecoverableStatePersistenceOptions, Request } from '@crawlee/core';
|
|
2
|
+
import type { Awaitable } from '@crawlee/types';
|
|
2
3
|
export type RenderingType = 'clientOnly' | 'static';
|
|
3
|
-
type URLComponents = string[];
|
|
4
|
-
type FeatureVector = [staticResultsSimilarity: number, clientOnlyResultsSimilarity: number];
|
|
5
4
|
export interface RenderingTypePredictorOptions {
|
|
6
5
|
/** A number between 0 and 1 that determines the desired ratio of rendering type detections */
|
|
7
6
|
detectionRatio: number;
|
|
7
|
+
persistenceOptions?: Partial<RecoverableStatePersistenceOptions>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Minimal contract that any object passed to {@link AdaptivePlaywrightCrawler} as its
|
|
11
|
+
* `renderingTypePredictor` option must satisfy.
|
|
12
|
+
*/
|
|
13
|
+
export interface IRenderingTypePredictor {
|
|
14
|
+
/**
|
|
15
|
+
* Predict the rendering type for a request, and how likely the crawler should be to verify it.
|
|
16
|
+
*
|
|
17
|
+
* Called once per request, before navigation - the crawler awaits it, so prefer loading whatever the
|
|
18
|
+
* prediction needs up front over per-request I/O.
|
|
19
|
+
*/
|
|
20
|
+
predict(request: Request): Awaitable<{
|
|
21
|
+
renderingType: RenderingType;
|
|
22
|
+
detectionProbabilityRecommendation: number;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* Report a rendering type established by a detection, or `clientOnly` when the `resultChecker` rejected an
|
|
26
|
+
* HTTP-only result, so that future predictions can take it into account.
|
|
27
|
+
*
|
|
28
|
+
* The crawler does not await this per detection - it collects the returned promises and drains them in
|
|
29
|
+
* `teardown()`, so an implementation that persists results asynchronously can keep batching its writes
|
|
30
|
+
* and still be sure they land before the crawl ends.
|
|
31
|
+
*/
|
|
32
|
+
storeResult(requests: Request | Request[], renderingType: RenderingType): Awaitable<void>;
|
|
8
33
|
}
|
|
9
34
|
/**
|
|
10
35
|
* 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.
|
|
11
|
-
*
|
|
12
|
-
* @experimental
|
|
13
36
|
*/
|
|
14
|
-
export declare class RenderingTypePredictor {
|
|
15
|
-
private
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
37
|
+
export declare class RenderingTypePredictor implements IRenderingTypePredictor {
|
|
38
|
+
#private;
|
|
39
|
+
constructor({ detectionRatio, persistenceOptions }: RenderingTypePredictorOptions);
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the predictor by restoring persisted state.
|
|
42
|
+
*/
|
|
43
|
+
initialize(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Stop persisting the model, writing it out one last time. `initialize()` reopens the persistence window.
|
|
46
|
+
*/
|
|
47
|
+
teardown(): Promise<void>;
|
|
48
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
19
49
|
/**
|
|
20
50
|
* Predict the rendering type for a given URL and request label.
|
|
21
51
|
*/
|
|
@@ -26,10 +56,5 @@ export declare class RenderingTypePredictor {
|
|
|
26
56
|
/**
|
|
27
57
|
* Store the rendering type for a given URL and request label. This updates the underlying prediction model, which may be costly.
|
|
28
58
|
*/
|
|
29
|
-
storeResult(
|
|
30
|
-
private resultCount;
|
|
31
|
-
protected calculateFeatureVector(url: URLComponents, label: string | undefined): FeatureVector;
|
|
32
|
-
protected retrain(): void;
|
|
59
|
+
storeResult(requests: Request | Request[], renderingType: RenderingType): void;
|
|
33
60
|
}
|
|
34
|
-
export {};
|
|
35
|
-
//# sourceMappingURL=rendering-type-prediction.d.ts.map
|