@crawlee/cheerio 3.0.0-beta.6 → 3.0.0-beta.60
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 +77 -130
- package/index.mjs +7 -0
- package/internals/cheerio-crawler.d.ts +84 -142
- package/internals/cheerio-crawler.d.ts.map +1 -1
- package/internals/cheerio-crawler.js +60 -22
- package/internals/cheerio-crawler.js.map +1 -1
- package/package.json +14 -10
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -1,132 +1,68 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
export declare type CheerioFailedRequestHandler<JSONData = Dictionary> = (inputs:
|
|
13
|
-
export interface CheerioCrawlerOptions<JSONData = Dictionary> extends Omit<BasicCrawlerOptions<CheerioCrawlingContext<JSONData>>, 'requestHandler' | 'handleRequestFunction' | 'failedRequestHandler' | 'handleFailedRequestFunction' | 'handleRequestTimeoutSecs'> {
|
|
3
|
+
import type { BasicCrawlerOptions } from '@crawlee/basic';
|
|
4
|
+
import { BasicCrawler } from '@crawlee/basic';
|
|
5
|
+
import type { CrawlingContext, EnqueueLinksOptions, ProxyConfiguration, ProxyInfo, Request, RequestQueue, Session } from '@crawlee/core';
|
|
6
|
+
import { CrawlerExtension } from '@crawlee/core';
|
|
7
|
+
import type { BatchAddRequestsResult, Awaitable, Dictionary } from '@crawlee/types';
|
|
8
|
+
import type { CheerioRoot } from '@crawlee/utils';
|
|
9
|
+
import type { RequestLike, ResponseLike } from 'content-type';
|
|
10
|
+
import type { OptionsInit, Response as GotResponse, GotOptionsInit } from 'got-scraping';
|
|
11
|
+
import type { IncomingMessage } from 'http';
|
|
12
|
+
export declare type CheerioFailedRequestHandler<JSONData = Dictionary> = (inputs: CheerioCrawlingContext<JSONData>, error: Error) => Awaitable<void>;
|
|
13
|
+
export interface CheerioCrawlerOptions<JSONData = Dictionary> extends Omit<BasicCrawlerOptions<CheerioCrawlingContext<JSONData>>, 'requestHandler' | 'handleRequestFunction' | 'failedRequestHandler' | 'handleFailedRequestFunction' | 'handleRequestTimeoutSecs' | 'errorHandler'> {
|
|
14
14
|
/**
|
|
15
15
|
* User-provided function that performs the logic of the crawler. It is called for each page
|
|
16
16
|
* loaded and parsed by the crawler.
|
|
17
17
|
*
|
|
18
|
-
* The function receives the
|
|
19
|
-
*
|
|
20
|
-
* {
|
|
21
|
-
* // The Cheerio object's function with the parsed HTML.
|
|
22
|
-
* $: Cheerio,
|
|
23
|
-
*
|
|
24
|
-
* // The request body of the web page, whose type depends on the content type.
|
|
25
|
-
* body: String|Buffer,
|
|
26
|
-
*
|
|
27
|
-
* // The parsed object from JSON for responses with the "application/json" content types.
|
|
28
|
-
* // For other content types it's null.
|
|
29
|
-
* json: Object,
|
|
30
|
-
*
|
|
31
|
-
* // Request object with details of the requested web page
|
|
32
|
-
* request: Request,
|
|
33
|
-
*
|
|
34
|
-
* // Parsed Content-Type HTTP header: { type, encoding }
|
|
35
|
-
* contentType: Object,
|
|
18
|
+
* The function receives the {@link CheerioCrawlingContext} as an argument,
|
|
19
|
+
* where the {@link CheerioCrawlingContext.request} instance represents the URL to crawl.
|
|
36
20
|
*
|
|
37
|
-
*
|
|
38
|
-
* response: Object,
|
|
39
|
-
*
|
|
40
|
-
* // Session object, useful to work around anti-scraping protections
|
|
41
|
-
* session: Session
|
|
42
|
-
*
|
|
43
|
-
* // ProxyInfo object with information about currently used proxy
|
|
44
|
-
* proxyInfo: ProxyInfo
|
|
45
|
-
*
|
|
46
|
-
* // The running cheerio crawler instance.
|
|
47
|
-
* crawler: CheerioCrawler
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* Type of `body` depends on the `Content-Type` header of the web page:
|
|
21
|
+
* Type of {@link CheerioCrawlingContext.body} depends on the `Content-Type` header of the web page:
|
|
52
22
|
* - String for `text/html`, `application/xhtml+xml`, `application/xml` MIME content types
|
|
53
23
|
* - Buffer for others MIME content types
|
|
54
24
|
*
|
|
55
25
|
* Parsed `Content-Type` header using
|
|
56
26
|
* [content-type package](https://www.npmjs.com/package/content-type)
|
|
57
|
-
* is stored in
|
|
27
|
+
* is stored in {@link CheerioCrawlingContext.contentType}`.
|
|
58
28
|
*
|
|
59
29
|
* Cheerio is available only for HTML and XML content types.
|
|
60
30
|
*
|
|
61
|
-
* With the {@link Request} object representing the URL to crawl.
|
|
62
|
-
*
|
|
63
31
|
* If the function returns, the returned promise is awaited by the crawler.
|
|
64
32
|
*
|
|
65
33
|
* If the function throws an exception, the crawler will try to re-crawl the
|
|
66
34
|
* request later, up to `option.maxRequestRetries` times.
|
|
67
35
|
* If all the retries fail, the crawler calls the function
|
|
68
|
-
* provided to the `
|
|
36
|
+
* provided to the `failedRequestHandler` parameter.
|
|
69
37
|
* To make this work, you should **always**
|
|
70
38
|
* let your function throw exceptions rather than catch them.
|
|
71
39
|
* The exceptions are logged to the request using the
|
|
72
40
|
* {@link Request.pushErrorMessage} function.
|
|
73
41
|
*/
|
|
74
|
-
requestHandler
|
|
42
|
+
requestHandler?: CheerioRequestHandler<JSONData>;
|
|
75
43
|
/**
|
|
76
44
|
* User-provided function that performs the logic of the crawler. It is called for each page
|
|
77
45
|
* loaded and parsed by the crawler.
|
|
78
46
|
*
|
|
79
|
-
* The function receives the
|
|
80
|
-
*
|
|
81
|
-
* {
|
|
82
|
-
* // The Cheerio object's function with the parsed HTML.
|
|
83
|
-
* $: Cheerio,
|
|
84
|
-
*
|
|
85
|
-
* // The request body of the web page, whose type depends on the content type.
|
|
86
|
-
* body: String|Buffer,
|
|
87
|
-
*
|
|
88
|
-
* // The parsed object from JSON for responses with the "application/json" content types.
|
|
89
|
-
* // For other content types it's null.
|
|
90
|
-
* json: Object,
|
|
91
|
-
*
|
|
92
|
-
* // Request object with details of the requested web page
|
|
93
|
-
* request: Request,
|
|
94
|
-
*
|
|
95
|
-
* // Parsed Content-Type HTTP header: { type, encoding }
|
|
96
|
-
* contentType: Object,
|
|
97
|
-
*
|
|
98
|
-
* // An instance of Node's http.IncomingMessage object,
|
|
99
|
-
* response: Object,
|
|
47
|
+
* The function receives the {@link CheerioCrawlingContext} as an argument,
|
|
48
|
+
* where the {@link CheerioCrawlingContext.request} instance represents the URL to crawl.
|
|
100
49
|
*
|
|
101
|
-
*
|
|
102
|
-
* session: Session
|
|
103
|
-
*
|
|
104
|
-
* // ProxyInfo object with information about currently used proxy
|
|
105
|
-
* proxyInfo: ProxyInfo
|
|
106
|
-
*
|
|
107
|
-
* // The running cheerio crawler instance.
|
|
108
|
-
* crawler: CheerioCrawler
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* Type of `body` depends on the `Content-Type` header of the web page:
|
|
50
|
+
* Type of {@link CheerioCrawlingContext.body} depends on the `Content-Type` header of the web page:
|
|
113
51
|
* - String for `text/html`, `application/xhtml+xml`, `application/xml` MIME content types
|
|
114
52
|
* - Buffer for others MIME content types
|
|
115
53
|
*
|
|
116
54
|
* Parsed `Content-Type` header using
|
|
117
55
|
* [content-type package](https://www.npmjs.com/package/content-type)
|
|
118
|
-
* is stored in
|
|
56
|
+
* is stored in {@link CheerioCrawlingContext.contentType}`.
|
|
119
57
|
*
|
|
120
58
|
* Cheerio is available only for HTML and XML content types.
|
|
121
59
|
*
|
|
122
|
-
* With the {@link Request} object representing the URL to crawl.
|
|
123
|
-
*
|
|
124
60
|
* If the function returns, the returned promise is awaited by the crawler.
|
|
125
61
|
*
|
|
126
62
|
* If the function throws an exception, the crawler will try to re-crawl the
|
|
127
63
|
* request later, up to `option.maxRequestRetries` times.
|
|
128
64
|
* If all the retries fail, the crawler calls the function
|
|
129
|
-
* provided to the `
|
|
65
|
+
* provided to the `failedRequestHandler` parameter.
|
|
130
66
|
* To make this work, you should **always**
|
|
131
67
|
* let your function throw exceptions rather than catch them.
|
|
132
68
|
* The exceptions are logged to the request using the
|
|
@@ -138,7 +74,7 @@ export interface CheerioCrawlerOptions<JSONData = Dictionary> extends Omit<Basic
|
|
|
138
74
|
/**
|
|
139
75
|
* Timeout in which the HTTP request to the resource needs to finish, given in seconds.
|
|
140
76
|
*/
|
|
141
|
-
|
|
77
|
+
navigationTimeoutSecs?: number;
|
|
142
78
|
/**
|
|
143
79
|
* If set to true, SSL certificate errors will be ignored.
|
|
144
80
|
*/
|
|
@@ -149,51 +85,37 @@ export interface CheerioCrawlerOptions<JSONData = Dictionary> extends Omit<Basic
|
|
|
149
85
|
* For more information, see the [documentation](https://docs.apify.com/proxy).
|
|
150
86
|
*/
|
|
151
87
|
proxyConfiguration?: ProxyConfiguration;
|
|
88
|
+
/**
|
|
89
|
+
* User-provided function that allows modifying the request object before it gets retried by the crawler.
|
|
90
|
+
* It's executed before each retry for the requests that failed less than `option.maxRequestRetries` times.
|
|
91
|
+
*
|
|
92
|
+
* The function receives the {@link CheerioCrawlingContext} as the first argument,
|
|
93
|
+
* where the {@link CheerioCrawlingContext.request} corresponds to the request to be retried.
|
|
94
|
+
* Second argument is the `Error` instance that
|
|
95
|
+
* represents the last error thrown during processing of the request.
|
|
96
|
+
*/
|
|
97
|
+
errorHandler?: CheerioFailedRequestHandler<JSONData>;
|
|
152
98
|
/**
|
|
153
99
|
* A function to handle requests that failed more than `option.maxRequestRetries` times.
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* {
|
|
157
|
-
*
|
|
158
|
-
* request: Request,
|
|
159
|
-
* session: Session,
|
|
160
|
-
* $: Cheerio,
|
|
161
|
-
* body: String|Buffer,
|
|
162
|
-
* json: Object,
|
|
163
|
-
* contentType: Object,
|
|
164
|
-
* response: Object,
|
|
165
|
-
* proxyInfo: ProxyInfo,
|
|
166
|
-
* crawler: CheerioCrawler,
|
|
167
|
-
* }
|
|
168
|
-
* ```
|
|
169
|
-
* where the {@link Request} instance corresponds to the failed request, and the `Error` instance
|
|
100
|
+
*
|
|
101
|
+
* The function receives the {@link CheerioCrawlingContext} as the first argument,
|
|
102
|
+
* where the {@link CheerioCrawlingContext.request} corresponds to the failed request.
|
|
103
|
+
* Second argument is the `Error` instance that
|
|
170
104
|
* represents the last error thrown during processing of the request.
|
|
171
105
|
*
|
|
172
|
-
* See [source code](https://github.com/apify/apify-
|
|
106
|
+
* See [source code](https://github.com/apify/apify-ts/blob/master/src/crawlers/cheerio_crawler.js#L13)
|
|
173
107
|
* for the default implementation of this function.
|
|
174
108
|
*/
|
|
175
109
|
failedRequestHandler?: CheerioFailedRequestHandler<JSONData>;
|
|
176
110
|
/**
|
|
177
111
|
* A function to handle requests that failed more than `option.maxRequestRetries` times.
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* {
|
|
181
|
-
*
|
|
182
|
-
* request: Request,
|
|
183
|
-
* session: Session,
|
|
184
|
-
* $: Cheerio,
|
|
185
|
-
* body: String|Buffer,
|
|
186
|
-
* json: Object,
|
|
187
|
-
* contentType: Object,
|
|
188
|
-
* response: Object,
|
|
189
|
-
* proxyInfo: ProxyInfo,
|
|
190
|
-
* crawler: CheerioCrawler,
|
|
191
|
-
* }
|
|
192
|
-
* ```
|
|
193
|
-
* where the {@link Request} instance corresponds to the failed request, and the `Error` instance
|
|
112
|
+
*
|
|
113
|
+
* The function receives the {@link CheerioCrawlingContext} as the first argument,
|
|
114
|
+
* where the {@link CheerioCrawlingContext.request} corresponds to the failed request.
|
|
115
|
+
* Second argument is the `Error` instance that
|
|
194
116
|
* represents the last error thrown during processing of the request.
|
|
195
117
|
*
|
|
196
|
-
* See [source code](https://github.com/apify/apify-
|
|
118
|
+
* See [source code](https://github.com/apify/apify-ts/blob/master/src/crawlers/cheerio_crawler.js#L13)
|
|
197
119
|
* for the default implementation of this function.
|
|
198
120
|
*
|
|
199
121
|
* @deprecated `handleFailedRequestFunction` has been renamed to `failedRequestHandler` and will be removed in a future version.
|
|
@@ -300,7 +222,7 @@ export interface PostResponseInputs<JSONData = Dictionary> {
|
|
|
300
222
|
crawler: CheerioCrawler<JSONData>;
|
|
301
223
|
}
|
|
302
224
|
export declare type PostResponse<JSONData = Dictionary> = (inputs: PostResponseInputs<JSONData>) => Awaitable<void>;
|
|
303
|
-
export interface
|
|
225
|
+
export interface CheerioCrawlingContext<JSONData extends Dictionary = Dictionary> extends CrawlingContext<JSONData> {
|
|
304
226
|
/**
|
|
305
227
|
* The [Cheerio](https://cheerio.js.org/) object with parsed HTML.
|
|
306
228
|
*/
|
|
@@ -323,9 +245,9 @@ export interface CheerioRequestHandlerInputs<JSONData = Dictionary> extends Craw
|
|
|
323
245
|
crawler: CheerioCrawler<JSONData>;
|
|
324
246
|
response: IncomingMessage;
|
|
325
247
|
enqueueLinks: (options?: CheerioCrawlerEnqueueLinksOptions) => Promise<BatchAddRequestsResult>;
|
|
248
|
+
sendRequest: (overrideOptions?: Partial<GotOptionsInit>) => Promise<GotResponse<string>>;
|
|
326
249
|
}
|
|
327
|
-
export declare type
|
|
328
|
-
export declare type CheerioRequestHandler<JSONData = Dictionary> = (inputs: CheerioRequestHandlerInputs<JSONData>) => Awaitable<void>;
|
|
250
|
+
export declare type CheerioRequestHandler<JSONData = Dictionary> = (inputs: CheerioCrawlingContext<JSONData>) => Awaitable<void>;
|
|
329
251
|
export declare type CheerioCrawlerEnqueueLinksOptions = Omit<EnqueueLinksOptions, 'urls' | 'requestQueue'>;
|
|
330
252
|
/**
|
|
331
253
|
* Provides a framework for the parallel crawling of web pages using plain HTTP requests and
|
|
@@ -380,18 +302,15 @@ export declare type CheerioCrawlerEnqueueLinksOptions = Omit<EnqueueLinksOptions
|
|
|
380
302
|
*
|
|
381
303
|
* ```javascript
|
|
382
304
|
* // Prepare a list of URLs to crawl
|
|
383
|
-
* const requestList =
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
* ],
|
|
388
|
-
* });
|
|
389
|
-
* await requestList.initialize();
|
|
305
|
+
* const requestList = await RequestList.open(null, [
|
|
306
|
+
* { url: 'http://www.example.com/page-1' },
|
|
307
|
+
* { url: 'http://www.example.com/page-2' },
|
|
308
|
+
* ]);
|
|
390
309
|
*
|
|
391
310
|
* // Crawl the URLs
|
|
392
311
|
* const crawler = new CheerioCrawler({
|
|
393
312
|
* requestList,
|
|
394
|
-
*
|
|
313
|
+
* async requestHandler({ request, response, body, contentType, $ }) {
|
|
395
314
|
* const data = [];
|
|
396
315
|
*
|
|
397
316
|
* // Do some data extraction from the page with Cheerio.
|
|
@@ -400,7 +319,7 @@ export declare type CheerioCrawlerEnqueueLinksOptions = Omit<EnqueueLinksOptions
|
|
|
400
319
|
* });
|
|
401
320
|
*
|
|
402
321
|
* // Save the data to dataset.
|
|
403
|
-
* await
|
|
322
|
+
* await Dataset.pushData({
|
|
404
323
|
* url: request.url,
|
|
405
324
|
* html: body,
|
|
406
325
|
* data,
|
|
@@ -412,7 +331,7 @@ export declare type CheerioCrawlerEnqueueLinksOptions = Omit<EnqueueLinksOptions
|
|
|
412
331
|
* ```
|
|
413
332
|
* @category Crawlers
|
|
414
333
|
*/
|
|
415
|
-
export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<CheerioCrawlingContext<JSONData
|
|
334
|
+
export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<CheerioCrawlingContext<JSONData>> {
|
|
416
335
|
/**
|
|
417
336
|
* A reference to the underlying {@link ProxyConfiguration} class that manages the crawler's proxies.
|
|
418
337
|
* Only available if used by the crawler.
|
|
@@ -422,14 +341,14 @@ export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<
|
|
|
422
341
|
protected preNavigationHooks: CheerioHook<JSONData>[];
|
|
423
342
|
protected postNavigationHooks: CheerioHook<JSONData>[];
|
|
424
343
|
protected persistCookiesPerSession: boolean;
|
|
425
|
-
protected
|
|
344
|
+
protected navigationTimeoutMillis: number;
|
|
426
345
|
protected ignoreSslErrors: boolean;
|
|
427
346
|
protected suggestResponseEncoding?: string;
|
|
428
347
|
protected forceResponseEncoding?: string;
|
|
429
348
|
protected readonly supportedMimeTypes: Set<string>;
|
|
430
349
|
protected static optionsShape: {
|
|
431
350
|
handlePageFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
432
|
-
|
|
351
|
+
navigationTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
433
352
|
ignoreSslErrors: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
434
353
|
additionalMimeTypes: import("ow").ArrayPredicate<string>;
|
|
435
354
|
suggestResponseEncoding: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
@@ -442,11 +361,9 @@ export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<
|
|
|
442
361
|
requestQueue: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
|
|
443
362
|
requestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
444
363
|
handleRequestFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
445
|
-
requestHandlerTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
446
|
-
* An object with information about currently used proxy by the crawler
|
|
447
|
-
* and configured by the {@link ProxyConfiguration} class.
|
|
448
|
-
*/
|
|
364
|
+
requestHandlerTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
449
365
|
handleRequestTimeoutSecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
366
|
+
errorHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
450
367
|
failedRequestHandler: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
451
368
|
handleFailedRequestFunction: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
|
|
452
369
|
maxRequestRetries: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
|
|
@@ -461,7 +378,7 @@ export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<
|
|
|
461
378
|
/**
|
|
462
379
|
* All `CheerioCrawler` parameters are passed via an options object.
|
|
463
380
|
*/
|
|
464
|
-
constructor(options
|
|
381
|
+
constructor(options?: CheerioCrawlerOptions<JSONData>);
|
|
465
382
|
/**
|
|
466
383
|
* **EXPERIMENTAL**
|
|
467
384
|
* Function for attaching CrawlerExtensions such as the Unblockers.
|
|
@@ -469,7 +386,7 @@ export declare class CheerioCrawler<JSONData = Dictionary> extends BasicCrawler<
|
|
|
469
386
|
*/
|
|
470
387
|
use(extension: CrawlerExtension): void;
|
|
471
388
|
/**
|
|
472
|
-
* Wrapper around
|
|
389
|
+
* Wrapper around requestHandler that opens and closes pages etc.
|
|
473
390
|
*/
|
|
474
391
|
protected _runRequestHandler(crawlingContext: CheerioCrawlingContext<JSONData>): Promise<void>;
|
|
475
392
|
protected _handleNavigation(crawlingContext: CheerioCrawlingContext<JSONData>): Promise<void>;
|
|
@@ -545,5 +462,30 @@ interface RequestFunctionOptions {
|
|
|
545
462
|
proxyUrl?: string;
|
|
546
463
|
gotOptions: OptionsInit;
|
|
547
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* Creates new {@link Router} instance that works based on request labels.
|
|
467
|
+
* This instance can then serve as a `requestHandler` of your {@link CheerioCrawler}.
|
|
468
|
+
* Defaults to the {@link CheerioCrawlingContext}.
|
|
469
|
+
*
|
|
470
|
+
* > Serves as a shortcut for using `Router.create<CheerioCrawlingContext>()`.
|
|
471
|
+
*
|
|
472
|
+
* ```ts
|
|
473
|
+
* import { CheerioCrawler, createCheerioRouter } from 'crawlee';
|
|
474
|
+
*
|
|
475
|
+
* const router = createCheerioRouter();
|
|
476
|
+
* router.addHandler('label-a', async (ctx) => {
|
|
477
|
+
* ctx.log.info('...');
|
|
478
|
+
* });
|
|
479
|
+
* router.addDefaultHandler(async (ctx) => {
|
|
480
|
+
* ctx.log.info('...');
|
|
481
|
+
* });
|
|
482
|
+
*
|
|
483
|
+
* const crawler = new CheerioCrawler({
|
|
484
|
+
* requestHandler: router,
|
|
485
|
+
* });
|
|
486
|
+
* await crawler.run();
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
489
|
+
export declare function createCheerioRouter<Context extends CheerioCrawlingContext = CheerioCrawlingContext>(): import("@crawlee/basic").RouterHandler<Context>;
|
|
548
490
|
export {};
|
|
549
491
|
//# sourceMappingURL=cheerio-crawler.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cheerio-crawler.d.ts","sourceRoot":"","sources":["../../src/internals/cheerio-crawler.ts"],"names":[],"mappings":";;AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"cheerio-crawler.d.ts","sourceRoot":"","sources":["../../src/internals/cheerio-crawler.ts"],"names":[],"mappings":";;AAEA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EACH,YAAY,EAEf,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EACR,eAAe,EACf,mBAAmB,EACnB,kBAAkB,EAClB,SAAS,EACT,OAAO,EACP,YAAY,EACZ,OAAO,EACV,MAAM,eAAe,CAAC;AACvB,OAAO,EACH,gBAAgB,EAMnB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,sBAAsB,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACpF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAIlD,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,KAAK,EAAE,WAAW,EAAiC,QAAQ,IAAI,WAAW,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAIxH,OAAO,KAAK,EAAuB,eAAe,EAAE,MAAM,MAAM,CAAC;AAoBjE,o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CAAC;KAC7B;cAqCe,eAAe,CAAC,QAAQ,EAAE,eAAe;IAczD;;OAEG;IACH,SAAS,CAAC,yBAAyB,CAAC,mBAAmB,EAAE,CAAC,MAAM,GAAG,WAAW,GAAG,YAAY,CAAC,EAAE;IAWhG;;OAEG;IACH,SAAS,CAAC,qBAAqB,CAAC,OAAO,CAAC,EAAE,OAAO;IAKjD,OAAO,CAAC,oBAAoB;IAgB5B;;OAEG;IACH,OAAO,CAAC,iBAAiB,CASvB;CACL;AAED,UAAU,2BAA2B;IACjC,OAAO,CAAC,EAAE,iCAAiC,CAAC;IAC5C,CAAC,EAAE,WAAW,GAAG,IAAI,CAAC;IACtB,YAAY,EAAE,YAAY,CAAC;IAC3B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,gBAAgB;AAChB,wBAAsB,0BAA0B,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,YAAY,EAAE,kBAAkB,EAAE,eAAe,EAAE,EAAE,2BAA2B,mCAoB9I;AAED,UAAU,sBAAsB;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,WAAW,CAAC;CAC3B;AAsED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,SAAS,sBAAsB,GAAG,sBAAsB,qDAElG"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cheerioCrawlerEnqueueLinks = exports.CheerioCrawler = void 0;
|
|
3
|
+
exports.createCheerioRouter = exports.cheerioCrawlerEnqueueLinks = exports.CheerioCrawler = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const timeout_1 = require("@apify/timeout");
|
|
6
6
|
const utilities_1 = require("@apify/utilities");
|
|
@@ -82,18 +82,15 @@ const CHEERIO_OPTIMIZED_AUTOSCALED_POOL_OPTIONS = {
|
|
|
82
82
|
*
|
|
83
83
|
* ```javascript
|
|
84
84
|
* // Prepare a list of URLs to crawl
|
|
85
|
-
* const requestList =
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
* ],
|
|
90
|
-
* });
|
|
91
|
-
* await requestList.initialize();
|
|
85
|
+
* const requestList = await RequestList.open(null, [
|
|
86
|
+
* { url: 'http://www.example.com/page-1' },
|
|
87
|
+
* { url: 'http://www.example.com/page-2' },
|
|
88
|
+
* ]);
|
|
92
89
|
*
|
|
93
90
|
* // Crawl the URLs
|
|
94
91
|
* const crawler = new CheerioCrawler({
|
|
95
92
|
* requestList,
|
|
96
|
-
*
|
|
93
|
+
* async requestHandler({ request, response, body, contentType, $ }) {
|
|
97
94
|
* const data = [];
|
|
98
95
|
*
|
|
99
96
|
* // Do some data extraction from the page with Cheerio.
|
|
@@ -102,7 +99,7 @@ const CHEERIO_OPTIMIZED_AUTOSCALED_POOL_OPTIONS = {
|
|
|
102
99
|
* });
|
|
103
100
|
*
|
|
104
101
|
* // Save the data to dataset.
|
|
105
|
-
* await
|
|
102
|
+
* await Dataset.pushData({
|
|
106
103
|
* url: request.url,
|
|
107
104
|
* html: body,
|
|
108
105
|
* data,
|
|
@@ -118,9 +115,9 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
118
115
|
/**
|
|
119
116
|
* All `CheerioCrawler` parameters are passed via an options object.
|
|
120
117
|
*/
|
|
121
|
-
constructor(options) {
|
|
118
|
+
constructor(options = {}) {
|
|
122
119
|
(0, ow_1.default)(options, 'CheerioCrawlerOptions', ow_1.default.object.exactShape(CheerioCrawler.optionsShape));
|
|
123
|
-
const { requestHandler, handlePageFunction, requestHandlerTimeoutSecs = 60,
|
|
120
|
+
const { requestHandler, handlePageFunction, requestHandlerTimeoutSecs = 60, navigationTimeoutSecs = 30, ignoreSslErrors = true, additionalMimeTypes = [], suggestResponseEncoding, forceResponseEncoding, proxyConfiguration, persistCookiesPerSession, preNavigationHooks = [], postNavigationHooks = [],
|
|
124
121
|
// Ignored
|
|
125
122
|
handleRequestFunction,
|
|
126
123
|
// BasicCrawler
|
|
@@ -132,7 +129,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
132
129
|
autoscaledPoolOptions,
|
|
133
130
|
// We need to add some time for internal functions to finish,
|
|
134
131
|
// but not too much so that we would stall the crawler.
|
|
135
|
-
requestHandlerTimeoutSecs:
|
|
132
|
+
requestHandlerTimeoutSecs: navigationTimeoutSecs + requestHandlerTimeoutSecs + basic_1.BASIC_CRAWLER_TIMEOUT_BUFFER_SECS,
|
|
136
133
|
});
|
|
137
134
|
/**
|
|
138
135
|
* A reference to the underlying {@link ProxyConfiguration} class that manages the crawler's proxies.
|
|
@@ -168,7 +165,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
168
165
|
writable: true,
|
|
169
166
|
value: void 0
|
|
170
167
|
});
|
|
171
|
-
Object.defineProperty(this, "
|
|
168
|
+
Object.defineProperty(this, "navigationTimeoutMillis", {
|
|
172
169
|
enumerable: true,
|
|
173
170
|
configurable: true,
|
|
174
171
|
writable: true,
|
|
@@ -221,7 +218,11 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
221
218
|
propertyKey: 'requestHandler',
|
|
222
219
|
newProperty: requestHandler,
|
|
223
220
|
oldProperty: handlePageFunction,
|
|
221
|
+
allowUndefined: true,
|
|
224
222
|
});
|
|
223
|
+
if (!this.requestHandler) {
|
|
224
|
+
this.requestHandler = this.router;
|
|
225
|
+
}
|
|
225
226
|
// Cookies should be persisted per session only if session pool is used
|
|
226
227
|
if (!this.useSessionPool && persistCookiesPerSession) {
|
|
227
228
|
throw new Error('You cannot use "persistCookiesPerSession" without "useSessionPool" set to true.');
|
|
@@ -233,7 +234,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
233
234
|
this.log.warning('Both forceResponseEncoding and suggestResponseEncoding options are set. Using forceResponseEncoding.');
|
|
234
235
|
}
|
|
235
236
|
this.userRequestHandlerTimeoutMillis = requestHandlerTimeoutSecs * 1000;
|
|
236
|
-
this.
|
|
237
|
+
this.navigationTimeoutMillis = navigationTimeoutSecs * 1000;
|
|
237
238
|
this.ignoreSslErrors = ignoreSslErrors;
|
|
238
239
|
this.suggestResponseEncoding = suggestResponseEncoding;
|
|
239
240
|
this.forceResponseEncoding = forceResponseEncoding;
|
|
@@ -275,7 +276,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
275
276
|
}
|
|
276
277
|
}
|
|
277
278
|
/**
|
|
278
|
-
* Wrapper around
|
|
279
|
+
* Wrapper around requestHandler that opens and closes pages etc.
|
|
279
280
|
*/
|
|
280
281
|
async _runRequestHandler(crawlingContext) {
|
|
281
282
|
const { request, session } = crawlingContext;
|
|
@@ -349,7 +350,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
349
350
|
const postNavigationHooksCookies = this._getCookieHeaderFromRequest(request);
|
|
350
351
|
this._applyCookies(crawlingContext, gotOptions, preNavigationHooksCookies, postNavigationHooksCookies);
|
|
351
352
|
const proxyUrl = crawlingContext.proxyInfo?.url;
|
|
352
|
-
crawlingContext.response = await (0, timeout_1.addTimeoutToPromise)(() => this._requestFunction({ request, session, proxyUrl, gotOptions }), this.
|
|
353
|
+
crawlingContext.response = await (0, timeout_1.addTimeoutToPromise)(() => this._requestFunction({ request, session, proxyUrl, gotOptions }), this.navigationTimeoutMillis, `request timed out after ${this.navigationTimeoutMillis / 1000} seconds.`);
|
|
353
354
|
(0, timeout_1.tryCancel)();
|
|
354
355
|
await this._executeHooks(this.postNavigationHooks, crawlingContext, gotOptions);
|
|
355
356
|
(0, timeout_1.tryCancel)();
|
|
@@ -421,7 +422,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
421
422
|
url: request.url,
|
|
422
423
|
method: request.method,
|
|
423
424
|
proxyUrl,
|
|
424
|
-
timeout: { request: this.
|
|
425
|
+
timeout: { request: this.navigationTimeoutMillis },
|
|
425
426
|
sessionToken: session,
|
|
426
427
|
...gotOptions,
|
|
427
428
|
headers: { ...request.headers, ...gotOptions?.headers },
|
|
@@ -434,7 +435,7 @@ class CheerioCrawler extends basic_1.BasicCrawler {
|
|
|
434
435
|
// TODO this is incorrect, the check for man in the middle needs to be done
|
|
435
436
|
// on individual proxy level, not on the `proxyConfiguration` level,
|
|
436
437
|
// because users can use normal + MITM proxies in a single configuration.
|
|
437
|
-
//
|
|
438
|
+
// Disable SSL verification for MITM proxies
|
|
438
439
|
if (this.proxyConfiguration && this.proxyConfiguration.isManInTheMiddle) {
|
|
439
440
|
requestOptions.https = {
|
|
440
441
|
...requestOptions.https,
|
|
@@ -534,7 +535,7 @@ Object.defineProperty(CheerioCrawler, "optionsShape", {
|
|
|
534
535
|
value: {
|
|
535
536
|
...basic_1.BasicCrawler.optionsShape,
|
|
536
537
|
handlePageFunction: ow_1.default.optional.function,
|
|
537
|
-
|
|
538
|
+
navigationTimeoutSecs: ow_1.default.optional.number,
|
|
538
539
|
ignoreSslErrors: ow_1.default.optional.boolean,
|
|
539
540
|
additionalMimeTypes: ow_1.default.optional.array.ofType(ow_1.default.string),
|
|
540
541
|
suggestResponseEncoding: ow_1.default.optional.string,
|
|
@@ -581,10 +582,19 @@ function extractUrlsFromCheerio($, selector, baseUrl) {
|
|
|
581
582
|
throw new Error(`An extracted URL: ${href} is relative and options.baseUrl is not set. `
|
|
582
583
|
+ 'Use options.baseUrl in enqueueLinks() to automatically resolve relative URLs.');
|
|
583
584
|
}
|
|
585
|
+
const tryAbsolute = () => {
|
|
586
|
+
try {
|
|
587
|
+
return (new URL(href, baseUrl)).href;
|
|
588
|
+
}
|
|
589
|
+
catch {
|
|
590
|
+
return undefined;
|
|
591
|
+
}
|
|
592
|
+
};
|
|
584
593
|
return baseUrl
|
|
585
|
-
? (
|
|
594
|
+
? tryAbsolute()
|
|
586
595
|
: href;
|
|
587
|
-
})
|
|
596
|
+
})
|
|
597
|
+
.filter((href) => !!href);
|
|
588
598
|
}
|
|
589
599
|
/**
|
|
590
600
|
* The stream object returned from got does not have the below properties.
|
|
@@ -618,4 +628,32 @@ function addResponsePropertiesToStream(stream) {
|
|
|
618
628
|
}
|
|
619
629
|
return stream;
|
|
620
630
|
}
|
|
631
|
+
/**
|
|
632
|
+
* Creates new {@link Router} instance that works based on request labels.
|
|
633
|
+
* This instance can then serve as a `requestHandler` of your {@link CheerioCrawler}.
|
|
634
|
+
* Defaults to the {@link CheerioCrawlingContext}.
|
|
635
|
+
*
|
|
636
|
+
* > Serves as a shortcut for using `Router.create<CheerioCrawlingContext>()`.
|
|
637
|
+
*
|
|
638
|
+
* ```ts
|
|
639
|
+
* import { CheerioCrawler, createCheerioRouter } from 'crawlee';
|
|
640
|
+
*
|
|
641
|
+
* const router = createCheerioRouter();
|
|
642
|
+
* router.addHandler('label-a', async (ctx) => {
|
|
643
|
+
* ctx.log.info('...');
|
|
644
|
+
* });
|
|
645
|
+
* router.addDefaultHandler(async (ctx) => {
|
|
646
|
+
* ctx.log.info('...');
|
|
647
|
+
* });
|
|
648
|
+
*
|
|
649
|
+
* const crawler = new CheerioCrawler({
|
|
650
|
+
* requestHandler: router,
|
|
651
|
+
* });
|
|
652
|
+
* await crawler.run();
|
|
653
|
+
* ```
|
|
654
|
+
*/
|
|
655
|
+
function createCheerioRouter() {
|
|
656
|
+
return core_1.Router.create();
|
|
657
|
+
}
|
|
658
|
+
exports.createCheerioRouter = createCheerioRouter;
|
|
621
659
|
//# sourceMappingURL=cheerio-crawler.js.map
|