@crawlee/playwright 4.0.0-rc.1 → 4.0.0-rc.3
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/internals/adaptive-playwright-crawler.d.ts +38 -16
- package/internals/adaptive-playwright-crawler.js +271 -123
- package/internals/playwright-browser-pool.d.ts +12 -28
- package/internals/playwright-browser-pool.js +0 -27
- package/internals/playwright-crawler.d.ts +8 -4
- package/internals/playwright-crawler.js +54 -13
- package/internals/playwright-launcher.js +2 -1
- package/internals/utils/playwright-utils.d.ts +26 -5
- package/internals/utils/playwright-utils.js +53 -35
- package/internals/utils/rendering-type-prediction.d.ts +11 -6
- package/internals/utils/rendering-type-prediction.js +6 -4
- package/package.json +17 -15
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { BrowserHook, LoadedRequest,
|
|
2
|
-
import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
|
|
1
|
+
import type { BrowserHook, LoadedRequest, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
|
|
2
|
+
import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, ExtractLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
|
|
3
3
|
import { BasicCrawler } from '@crawlee/basic';
|
|
4
|
-
import
|
|
4
|
+
import { CrawlingRequest } from '@crawlee/browser';
|
|
5
|
+
import type { AddRequestsBatchedResult, StorageTransactionView } from '@crawlee/core';
|
|
5
6
|
import type { Dictionary, Awaitable } from '@crawlee/types';
|
|
6
7
|
import { type Cheerio, type CheerioAPI } from 'cheerio';
|
|
7
8
|
import type { AnyNode } from 'domhandler';
|
|
@@ -43,16 +44,17 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
|
|
|
43
44
|
* Playwright Page object. If accessed in HTTP-only rendering, this will throw an error and make the AdaptivePlaywrightCrawlerContext retry the request in a browser.
|
|
44
45
|
*/
|
|
45
46
|
page: Page;
|
|
46
|
-
/**
|
|
47
|
-
* Wait for an element matching the selector to appear and return a Cheerio object of the first matched element.
|
|
48
|
-
* Timeout defaults to 5s.
|
|
49
|
-
*/
|
|
50
|
-
querySelector(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
|
|
51
47
|
/**
|
|
52
48
|
* Wait for an element matching the selector to appear and return a Cheerio object of all matched elements.
|
|
49
|
+
* Throws when nothing matches - in the HTTP-only run, that makes the crawler retry the request in a browser.
|
|
53
50
|
* Timeout defaults to 5s.
|
|
54
51
|
*/
|
|
55
52
|
querySelectorAll(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
|
|
53
|
+
/**
|
|
54
|
+
* Like {@link AdaptivePlaywrightCrawlerContext.querySelectorAll|`querySelectorAll`}, but returns only the
|
|
55
|
+
* first match.
|
|
56
|
+
*/
|
|
57
|
+
querySelectorOne(selector: string, timeoutMs?: number): Promise<Cheerio<AnyNode>>;
|
|
56
58
|
/**
|
|
57
59
|
* Wait for an element matching the selector to appear.
|
|
58
60
|
* Timeout defaults to 5s.
|
|
@@ -80,9 +82,20 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
|
|
|
80
82
|
* ```
|
|
81
83
|
*/
|
|
82
84
|
parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
|
|
83
|
-
|
|
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>;
|
|
84
97
|
}
|
|
85
|
-
interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log'> {
|
|
98
|
+
interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log' | 'extendTimeout'> {
|
|
86
99
|
page?: Page;
|
|
87
100
|
request: CrawlingRequest;
|
|
88
101
|
gotoOptions?: PlaywrightGotoOptions;
|
|
@@ -91,7 +104,18 @@ type AdaptiveHook<ContextExtension = Dictionary<never>> = BrowserHook<AdaptiveHo
|
|
|
91
104
|
type AdaptivePostNavigationHook<ContextExtension = Dictionary<never>> = BrowserHook<Omit<AdaptiveHookContext, 'request'> & {
|
|
92
105
|
request: LoadedRequest<CrawlingRequest>;
|
|
93
106
|
}, ContextExtension>;
|
|
107
|
+
/** Passed to {@link AdaptivePlaywrightCrawlerOptions.resultChecker} and `resultComparator` alongside the results. */
|
|
108
|
+
export interface AdaptiveResultContext {
|
|
109
|
+
/** The request whose handler produced the results. */
|
|
110
|
+
request: CrawlingRequest;
|
|
111
|
+
}
|
|
94
112
|
export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends Omit<BasicCrawlerOptions<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension>, 'preNavigationHooks' | 'postNavigationHooks' | 'contextPipelineBuilder'>, Pick<PlaywrightCrawlerOptions, 'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser'> {
|
|
113
|
+
/**
|
|
114
|
+
* Timeout for the navigation phase of each attempt, in seconds, shared by the `preNavigationHooks`, the
|
|
115
|
+
* navigation and the `postNavigationHooks`. Applies to both the HTTP-only and the browser attempt. When not
|
|
116
|
+
* set, each attempt keeps its own default: 30 seconds for HTTP-only and 60 seconds for the browser.
|
|
117
|
+
*/
|
|
118
|
+
navigationTimeoutSecs?: number;
|
|
95
119
|
/**
|
|
96
120
|
* Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies.
|
|
97
121
|
* The function accepts a subset of the crawling context. If you attempt to access the `page` property during HTTP-only crawling,
|
|
@@ -121,7 +145,7 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
|
|
|
121
145
|
* If it returns false, the request is retried in a browser.
|
|
122
146
|
* If no callback is specified, every result is considered valid.
|
|
123
147
|
*/
|
|
124
|
-
resultChecker?: (result: StorageTransactionView) => boolean;
|
|
148
|
+
resultChecker?: (result: StorageTransactionView, context: AdaptiveResultContext) => boolean;
|
|
125
149
|
/**
|
|
126
150
|
* An optional callback that decides whether an error thrown during the plain HTTP request handler
|
|
127
151
|
* should be propagated (instead of falling back to browser navigation).
|
|
@@ -144,7 +168,7 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
|
|
|
144
168
|
*
|
|
145
169
|
* For a stricter, ready-made comparator that also takes enqueued requests and key-value store changes into account, see {@link fullResultComparator}.
|
|
146
170
|
*/
|
|
147
|
-
resultComparator?: (resultA: StorageTransactionView, resultB: StorageTransactionView) => boolean | 'equal' | 'different' | 'inconclusive';
|
|
171
|
+
resultComparator?: (resultA: StorageTransactionView, resultB: StorageTransactionView, context: AdaptiveResultContext) => boolean | 'equal' | 'different' | 'inconclusive';
|
|
148
172
|
/**
|
|
149
173
|
* A custom rendering type predictor. A predictor passed here is borrowed - the crawler never drives its
|
|
150
174
|
* lifecycle, so set it up yourself (the built-in {@link RenderingTypePredictor} needs `initialize()`).
|
|
@@ -160,9 +184,9 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
|
|
|
160
184
|
* ```javascript
|
|
161
185
|
* const crawler = new AdaptivePlaywrightCrawler({
|
|
162
186
|
* renderingTypeDetectionRatio: 0.1,
|
|
163
|
-
* async requestHandler({
|
|
187
|
+
* async requestHandler({ querySelectorAll, pushData, enqueueLinks, request, log }) {
|
|
164
188
|
* // This function is called to extract data from a single web page
|
|
165
|
-
* const $prices = await
|
|
189
|
+
* const $prices = await querySelectorAll('span.price')
|
|
166
190
|
*
|
|
167
191
|
* await pushData({
|
|
168
192
|
* url: request.url,
|
|
@@ -178,8 +202,6 @@ export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<
|
|
|
178
202
|
* 'http://www.example.com/page-2',
|
|
179
203
|
* ]);
|
|
180
204
|
* ```
|
|
181
|
-
*
|
|
182
|
-
* @experimental
|
|
183
205
|
*/
|
|
184
206
|
export declare class AdaptivePlaywrightCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends BasicCrawler<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension> {
|
|
185
207
|
#private;
|