@crawlbrulee/sdk 1.2.0 → 1.3.0
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 +64 -25
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +142 -18
- package/dist/index.d.ts +142 -18
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -257,6 +257,102 @@ interface ApiErrorResponse {
|
|
|
257
257
|
}
|
|
258
258
|
//#endregion
|
|
259
259
|
//#region src/types/scrape.d.ts
|
|
260
|
+
/**
|
|
261
|
+
* What an `extract.elements` spec reads from each match: its `text` (the
|
|
262
|
+
* default), its outer `html`, or one `attribute`.
|
|
263
|
+
*/
|
|
264
|
+
type ScrapeElementOutput = 'text' | 'html' | 'attribute';
|
|
265
|
+
/** What every spec has, with or without `fields`. */
|
|
266
|
+
interface ScrapeElementSpecBase {
|
|
267
|
+
/** CSS selector. At most 500 characters. */
|
|
268
|
+
selector: string;
|
|
269
|
+
/** Return every match as a list instead of the first match. Default `false`. */
|
|
270
|
+
all?: boolean;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* A spec that reads one value from each match, with no `fields`. It is also the
|
|
274
|
+
* innermost spec: {@link ScrapeElementSpec} nests `fields` up to 3 levels, and a
|
|
275
|
+
* spec inside the third `fields` must be this one.
|
|
276
|
+
*
|
|
277
|
+
* See https://crawlbrulee.com/docs/scrape/elements for the selector rules and limits.
|
|
278
|
+
*/
|
|
279
|
+
interface ScrapeElementLeafSpec extends ScrapeElementSpecBase {
|
|
280
|
+
/** What to read from the match. Default `text`. */
|
|
281
|
+
output?: ScrapeElementOutput;
|
|
282
|
+
/**
|
|
283
|
+
* The attribute to read, at most 100 characters. Required when `output` is
|
|
284
|
+
* `attribute`, and not allowed with any other `output`. `href` and `src` come
|
|
285
|
+
* back as full urls. A match without the attribute is `null`, and is left out
|
|
286
|
+
* of an `all: true` list.
|
|
287
|
+
*/
|
|
288
|
+
attribute?: string;
|
|
289
|
+
/** Not allowed here. Use a spec with `fields` instead. */
|
|
290
|
+
fields?: never;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A spec with `fields`: each match becomes an object. `fields` can't be
|
|
294
|
+
* combined with `output` or `attribute`, and an empty `fields` is refused.
|
|
295
|
+
*/
|
|
296
|
+
interface ScrapeElementSpecWithFields<Field> extends ScrapeElementSpecBase {
|
|
297
|
+
/** Values read inside each match, by name. At least one. */
|
|
298
|
+
fields: Record<string, string | Field>;
|
|
299
|
+
/** Not allowed with `fields`. */
|
|
300
|
+
output?: never;
|
|
301
|
+
/** Not allowed with `fields`. */
|
|
302
|
+
attribute?: never;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* A spec inside the second `fields`. If it has `fields` of its own, that is the
|
|
306
|
+
* third and last `fields`, and the specs inside it have no `fields`.
|
|
307
|
+
*/
|
|
308
|
+
type ScrapeElementNestedFieldSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementLeafSpec>;
|
|
309
|
+
/** A spec inside the first `fields` (the one on a {@link ScrapeElementSpec}). */
|
|
310
|
+
type ScrapeElementFieldSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementNestedFieldSpec>;
|
|
311
|
+
/**
|
|
312
|
+
* The full spec for one name in `extract.elements`.
|
|
313
|
+
*
|
|
314
|
+
* With `fields` (instead of `output` and `attribute`, never both), each match
|
|
315
|
+
* becomes an object: every field is a name and a selector read inside that
|
|
316
|
+
* match only. So a list of product cards can return a title, price and url per
|
|
317
|
+
* card. An empty `fields` is refused. Fields nest up to 3 levels; the types
|
|
318
|
+
* stop you from going deeper.
|
|
319
|
+
*
|
|
320
|
+
* See https://crawlbrulee.com/docs/scrape/elements for the selector rules and limits.
|
|
321
|
+
*/
|
|
322
|
+
type ScrapeElementSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementFieldSpec>;
|
|
323
|
+
/**
|
|
324
|
+
* Named values to read from the page by CSS selector, sent as
|
|
325
|
+
* `extract.elements`. Each key is a name you pick; each value is a selector
|
|
326
|
+
* string (the text of the first match) or a {@link ScrapeElementSpec}. The
|
|
327
|
+
* values come back on {@link ScrapeResponse.elements} under the same names.
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* const elements: ScrapeElements = {
|
|
331
|
+
* heading: 'h1',
|
|
332
|
+
* books: {
|
|
333
|
+
* selector: 'article.product_pod',
|
|
334
|
+
* all: true,
|
|
335
|
+
* fields: {
|
|
336
|
+
* title: { selector: 'h3 a', output: 'attribute', attribute: 'title' },
|
|
337
|
+
* price: '.price_color',
|
|
338
|
+
* },
|
|
339
|
+
* },
|
|
340
|
+
* }
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* See https://crawlbrulee.com/docs/scrape/elements.
|
|
344
|
+
*/
|
|
345
|
+
type ScrapeElements = Record<string, string | ScrapeElementSpec>;
|
|
346
|
+
/** One match read with `fields`: field name → value. */
|
|
347
|
+
interface ScrapeElementObject {
|
|
348
|
+
[name: string]: ScrapeElementValue;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* The value for one name in {@link ScrapeResponse.elements}: a string, an
|
|
352
|
+
* object (a spec with `fields`), a list of either (`all: true`), or `null` when
|
|
353
|
+
* nothing matched (`[]` with `all: true`).
|
|
354
|
+
*/
|
|
355
|
+
type ScrapeElementValue = string | ScrapeElementObject | (string | ScrapeElementObject)[] | null;
|
|
260
356
|
/**
|
|
261
357
|
* Which content formats to extract from the scraped page. Every field is
|
|
262
358
|
* optional; the server defaults are noted on each field. The default request
|
|
@@ -292,14 +388,22 @@ interface ScrapeExtract {
|
|
|
292
388
|
* `inline_images_truncated` warning is returned.
|
|
293
389
|
*/
|
|
294
390
|
images?: boolean;
|
|
391
|
+
/**
|
|
392
|
+
* Named values to read from the page by CSS selector. They come back on the
|
|
393
|
+
* top-level `elements` field under the same names. Read after `cleanup`, from
|
|
394
|
+
* the same page as `links` and `images`. No extra credits, also when the
|
|
395
|
+
* page comes from cache. See {@link ScrapeElements} and
|
|
396
|
+
* https://crawlbrulee.com/docs/scrape/elements.
|
|
397
|
+
*/
|
|
398
|
+
elements?: ScrapeElements;
|
|
295
399
|
/** Capture a screenshot. Omit to skip; set to a `ScreenshotRequest` to enable. */
|
|
296
400
|
screenshot?: ScreenshotRequest;
|
|
297
401
|
}
|
|
298
402
|
/**
|
|
299
403
|
* What is removed from the page before any output is built.
|
|
300
404
|
*
|
|
301
|
-
* Applies to `markdown`, `cleaned_html`, `links` and `
|
|
302
|
-
* and to the screenshot. It never applies to `raw_html` — that is always the
|
|
405
|
+
* Applies to `markdown`, `cleaned_html`, `links`, `images` and `elements` on
|
|
406
|
+
* every engine, and to the screenshot. It never applies to `raw_html` — that is always the
|
|
303
407
|
* page as it arrived, before anything was removed.
|
|
304
408
|
*/
|
|
305
409
|
interface ScrapeCleanup {
|
|
@@ -313,18 +417,18 @@ interface ScrapeCleanup {
|
|
|
313
417
|
* CSS selectors whose elements are removed before anything is captured. Use
|
|
314
418
|
* it for a banner or widget `ads_and_popups` does not recognise.
|
|
315
419
|
*
|
|
316
|
-
* At most 100 selectors, each at most 500 characters.
|
|
317
|
-
*
|
|
420
|
+
* At most 100 selectors, each at most 500 characters. A cached result is
|
|
421
|
+
* only reused for a request that removes the same selectors.
|
|
318
422
|
*/
|
|
319
423
|
exclude_selectors?: string[];
|
|
320
424
|
}
|
|
321
425
|
/**
|
|
322
426
|
* Cache settings for a scrape request. `max_age` is the only cache control.
|
|
323
427
|
* A cached result has to match the url, the screenshot setup (type, viewport,
|
|
324
|
-
* device mode), `cleanup.ads_and_popups
|
|
325
|
-
* `require_js: true` only matches browser-rendered
|
|
326
|
-
*
|
|
327
|
-
*
|
|
428
|
+
* device mode), `cleanup.ads_and_popups`, `cleanup.exclude_selectors` and
|
|
429
|
+
* `location.locale`, and `require_js: true` only matches browser-rendered
|
|
430
|
+
* results. A non-zero `actions_before` wait or scroll disables caching for that
|
|
431
|
+
* request.
|
|
328
432
|
*/
|
|
329
433
|
interface ScrapeCache {
|
|
330
434
|
/**
|
|
@@ -366,7 +470,7 @@ interface ScrapeRequest {
|
|
|
366
470
|
require_js?: boolean;
|
|
367
471
|
/**
|
|
368
472
|
* What is removed from the page before any output is built. Shapes
|
|
369
|
-
* `markdown`, `cleaned_html`, `links`, `images` and the screenshot.
|
|
473
|
+
* `markdown`, `cleaned_html`, `links`, `images`, `elements` and the screenshot.
|
|
370
474
|
*
|
|
371
475
|
* Never applies to `raw_html`, which is always the page before anything was
|
|
372
476
|
* removed.
|
|
@@ -444,7 +548,10 @@ interface ScreenshotProperties {
|
|
|
444
548
|
interface ScreenshotSlice {
|
|
445
549
|
/** 0-based row index of this slice. */
|
|
446
550
|
row_nr: number;
|
|
447
|
-
/**
|
|
551
|
+
/**
|
|
552
|
+
* Signed URL to download this slice image. Expires 24 hours after the scrape (for an async scrape, 24 hours after
|
|
553
|
+
* it was submitted). Download the image; don't keep the link.
|
|
554
|
+
*/
|
|
448
555
|
url: string;
|
|
449
556
|
type: 'slice';
|
|
450
557
|
/** Image-level properties of this slice. */
|
|
@@ -452,7 +559,10 @@ interface ScreenshotSlice {
|
|
|
452
559
|
}
|
|
453
560
|
/** Result block returned when a screenshot was requested. */
|
|
454
561
|
interface ScreenshotResult {
|
|
455
|
-
/**
|
|
562
|
+
/**
|
|
563
|
+
* Signed URL to download the full screenshot image. Expires 24 hours after the scrape (for an async scrape, 24 hours
|
|
564
|
+
* after it was submitted). Download the image; don't keep the link.
|
|
565
|
+
*/
|
|
456
566
|
url: string;
|
|
457
567
|
/** Capture mode that was used. */
|
|
458
568
|
type: ScreenshotType;
|
|
@@ -523,8 +633,9 @@ interface ScrapeMetadata {
|
|
|
523
633
|
* - `links_truncated` — the page had more than 30 000 links.
|
|
524
634
|
* - `inline_images_truncated` — the page had more than 10 000 inline images.
|
|
525
635
|
* - `raw_html_truncated` — the page body exceeded 10 000 000 characters of HTML.
|
|
526
|
-
* - `
|
|
527
|
-
*
|
|
636
|
+
* - `elements_truncated` — at least one `elements` value hit a limit: a list
|
|
637
|
+
* was cut, or a value too large to return is `null` (never cut short). See
|
|
638
|
+
* https://crawlbrulee.com/docs/scrape/elements for the limits.
|
|
528
639
|
*
|
|
529
640
|
* Unavailability — that section's extraction failed, so the field is omitted
|
|
530
641
|
* or empty while the rest of the scrape succeeded. These let you tell "the page
|
|
@@ -533,11 +644,17 @@ interface ScrapeMetadata {
|
|
|
533
644
|
* - `links_unavailable` — link extraction failed.
|
|
534
645
|
* - `inline_images_unavailable` — image extraction failed.
|
|
535
646
|
* - `metadata_unavailable` — metadata extraction failed.
|
|
647
|
+
* - `screenshot_unavailable` — a screenshot was asked for, but the page came
|
|
648
|
+
* back without one. The rest of the result is there.
|
|
536
649
|
*
|
|
537
650
|
* The page body has no such code: if it can't be extracted the scrape fails
|
|
538
651
|
* outright rather than returning a hollow `200`, and isn't billed.
|
|
652
|
+
*
|
|
653
|
+
* `metadata_truncated` is deprecated: metadata no longer has a size limit of
|
|
654
|
+
* its own, so new scrapes never send it. It stays in the union because results
|
|
655
|
+
* stored before that change can still carry it. Don't rely on it in new code.
|
|
539
656
|
*/
|
|
540
|
-
type ScrapeWarningCode = 'screenshot_truncated' | 'links_truncated' | 'inline_images_truncated' | 'raw_html_truncated' | 'metadata_truncated' | 'links_unavailable' | 'inline_images_unavailable' | 'metadata_unavailable';
|
|
657
|
+
type ScrapeWarningCode = 'screenshot_truncated' | 'links_truncated' | 'inline_images_truncated' | 'raw_html_truncated' | 'metadata_truncated' | 'elements_truncated' | 'links_unavailable' | 'inline_images_unavailable' | 'metadata_unavailable' | 'screenshot_unavailable';
|
|
541
658
|
/**
|
|
542
659
|
* Successful response from `POST /api/scrape` and `GET /api/scrape/result/:jobId`.
|
|
543
660
|
*
|
|
@@ -575,7 +692,7 @@ interface ScrapeResponse {
|
|
|
575
692
|
content_type?: string;
|
|
576
693
|
/**
|
|
577
694
|
* Extract fields that were requested but aren't supported for this
|
|
578
|
-
* content type (e.g. asking for `
|
|
695
|
+
* content type (e.g. asking for `metadata` or `elements` of a JSON file).
|
|
579
696
|
*/
|
|
580
697
|
unsupported_fields?: string[];
|
|
581
698
|
/** Page content converted to clean Markdown (when `extract.markdown`). */
|
|
@@ -591,11 +708,18 @@ interface ScrapeResponse {
|
|
|
591
708
|
images?: PageInlineImage[];
|
|
592
709
|
/** Links discovered on the page (when `extract.links`). */
|
|
593
710
|
links?: PageLink[];
|
|
711
|
+
/**
|
|
712
|
+
* The values asked for in `extract.elements`, under the same names. Every
|
|
713
|
+
* requested name is present, at every level of `fields`; a name with no
|
|
714
|
+
* match is `null` (`[]` with `all: true`). See {@link ScrapeElementValue}.
|
|
715
|
+
*/
|
|
716
|
+
elements?: Record<string, ScrapeElementValue>;
|
|
594
717
|
/**
|
|
595
718
|
* Captured screenshot (when `extract.screenshot`). In rare cases a screenshot
|
|
596
719
|
* can't be captured; when you also requested other outputs those are still
|
|
597
|
-
* returned and this field is
|
|
598
|
-
* `
|
|
720
|
+
* returned and this field is left out (so it reads back as `undefined`), with
|
|
721
|
+
* a `screenshot_unavailable` warning on `warnings` to tell this case apart —
|
|
722
|
+
* guard with `page.screenshot?.url`. A screenshot-only request
|
|
599
723
|
* that can't deliver fails instead of returning an empty response: a `422`
|
|
600
724
|
* with `unsupported_screenshot_output` when the content type can't be
|
|
601
725
|
* screenshotted, a `500` when the capture itself failed — and isn't billed.
|
|
@@ -1442,5 +1566,5 @@ interface VerifyWebhookSignatureOptions {
|
|
|
1442
1566
|
*/
|
|
1443
1567
|
declare function verifyWebhookSignature(options: VerifyWebhookSignatureOptions): Promise<WebhookVerificationResult>;
|
|
1444
1568
|
//#endregion
|
|
1445
|
-
export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TargetUnreachableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, ZeroDataRetentionNotEnabledError, isCrawlbruleeError, verifyWebhookSignature };
|
|
1569
|
+
export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeElementFieldSpec, ScrapeElementLeafSpec, ScrapeElementNestedFieldSpec, ScrapeElementObject, ScrapeElementOutput, ScrapeElementSpec, ScrapeElementValue, ScrapeElements, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TargetUnreachableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, ZeroDataRetentionNotEnabledError, isCrawlbruleeError, verifyWebhookSignature };
|
|
1446
1570
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -257,6 +257,102 @@ interface ApiErrorResponse {
|
|
|
257
257
|
}
|
|
258
258
|
//#endregion
|
|
259
259
|
//#region src/types/scrape.d.ts
|
|
260
|
+
/**
|
|
261
|
+
* What an `extract.elements` spec reads from each match: its `text` (the
|
|
262
|
+
* default), its outer `html`, or one `attribute`.
|
|
263
|
+
*/
|
|
264
|
+
type ScrapeElementOutput = 'text' | 'html' | 'attribute';
|
|
265
|
+
/** What every spec has, with or without `fields`. */
|
|
266
|
+
interface ScrapeElementSpecBase {
|
|
267
|
+
/** CSS selector. At most 500 characters. */
|
|
268
|
+
selector: string;
|
|
269
|
+
/** Return every match as a list instead of the first match. Default `false`. */
|
|
270
|
+
all?: boolean;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* A spec that reads one value from each match, with no `fields`. It is also the
|
|
274
|
+
* innermost spec: {@link ScrapeElementSpec} nests `fields` up to 3 levels, and a
|
|
275
|
+
* spec inside the third `fields` must be this one.
|
|
276
|
+
*
|
|
277
|
+
* See https://crawlbrulee.com/docs/scrape/elements for the selector rules and limits.
|
|
278
|
+
*/
|
|
279
|
+
interface ScrapeElementLeafSpec extends ScrapeElementSpecBase {
|
|
280
|
+
/** What to read from the match. Default `text`. */
|
|
281
|
+
output?: ScrapeElementOutput;
|
|
282
|
+
/**
|
|
283
|
+
* The attribute to read, at most 100 characters. Required when `output` is
|
|
284
|
+
* `attribute`, and not allowed with any other `output`. `href` and `src` come
|
|
285
|
+
* back as full urls. A match without the attribute is `null`, and is left out
|
|
286
|
+
* of an `all: true` list.
|
|
287
|
+
*/
|
|
288
|
+
attribute?: string;
|
|
289
|
+
/** Not allowed here. Use a spec with `fields` instead. */
|
|
290
|
+
fields?: never;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* A spec with `fields`: each match becomes an object. `fields` can't be
|
|
294
|
+
* combined with `output` or `attribute`, and an empty `fields` is refused.
|
|
295
|
+
*/
|
|
296
|
+
interface ScrapeElementSpecWithFields<Field> extends ScrapeElementSpecBase {
|
|
297
|
+
/** Values read inside each match, by name. At least one. */
|
|
298
|
+
fields: Record<string, string | Field>;
|
|
299
|
+
/** Not allowed with `fields`. */
|
|
300
|
+
output?: never;
|
|
301
|
+
/** Not allowed with `fields`. */
|
|
302
|
+
attribute?: never;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* A spec inside the second `fields`. If it has `fields` of its own, that is the
|
|
306
|
+
* third and last `fields`, and the specs inside it have no `fields`.
|
|
307
|
+
*/
|
|
308
|
+
type ScrapeElementNestedFieldSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementLeafSpec>;
|
|
309
|
+
/** A spec inside the first `fields` (the one on a {@link ScrapeElementSpec}). */
|
|
310
|
+
type ScrapeElementFieldSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementNestedFieldSpec>;
|
|
311
|
+
/**
|
|
312
|
+
* The full spec for one name in `extract.elements`.
|
|
313
|
+
*
|
|
314
|
+
* With `fields` (instead of `output` and `attribute`, never both), each match
|
|
315
|
+
* becomes an object: every field is a name and a selector read inside that
|
|
316
|
+
* match only. So a list of product cards can return a title, price and url per
|
|
317
|
+
* card. An empty `fields` is refused. Fields nest up to 3 levels; the types
|
|
318
|
+
* stop you from going deeper.
|
|
319
|
+
*
|
|
320
|
+
* See https://crawlbrulee.com/docs/scrape/elements for the selector rules and limits.
|
|
321
|
+
*/
|
|
322
|
+
type ScrapeElementSpec = ScrapeElementLeafSpec | ScrapeElementSpecWithFields<ScrapeElementFieldSpec>;
|
|
323
|
+
/**
|
|
324
|
+
* Named values to read from the page by CSS selector, sent as
|
|
325
|
+
* `extract.elements`. Each key is a name you pick; each value is a selector
|
|
326
|
+
* string (the text of the first match) or a {@link ScrapeElementSpec}. The
|
|
327
|
+
* values come back on {@link ScrapeResponse.elements} under the same names.
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* const elements: ScrapeElements = {
|
|
331
|
+
* heading: 'h1',
|
|
332
|
+
* books: {
|
|
333
|
+
* selector: 'article.product_pod',
|
|
334
|
+
* all: true,
|
|
335
|
+
* fields: {
|
|
336
|
+
* title: { selector: 'h3 a', output: 'attribute', attribute: 'title' },
|
|
337
|
+
* price: '.price_color',
|
|
338
|
+
* },
|
|
339
|
+
* },
|
|
340
|
+
* }
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* See https://crawlbrulee.com/docs/scrape/elements.
|
|
344
|
+
*/
|
|
345
|
+
type ScrapeElements = Record<string, string | ScrapeElementSpec>;
|
|
346
|
+
/** One match read with `fields`: field name → value. */
|
|
347
|
+
interface ScrapeElementObject {
|
|
348
|
+
[name: string]: ScrapeElementValue;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* The value for one name in {@link ScrapeResponse.elements}: a string, an
|
|
352
|
+
* object (a spec with `fields`), a list of either (`all: true`), or `null` when
|
|
353
|
+
* nothing matched (`[]` with `all: true`).
|
|
354
|
+
*/
|
|
355
|
+
type ScrapeElementValue = string | ScrapeElementObject | (string | ScrapeElementObject)[] | null;
|
|
260
356
|
/**
|
|
261
357
|
* Which content formats to extract from the scraped page. Every field is
|
|
262
358
|
* optional; the server defaults are noted on each field. The default request
|
|
@@ -292,14 +388,22 @@ interface ScrapeExtract {
|
|
|
292
388
|
* `inline_images_truncated` warning is returned.
|
|
293
389
|
*/
|
|
294
390
|
images?: boolean;
|
|
391
|
+
/**
|
|
392
|
+
* Named values to read from the page by CSS selector. They come back on the
|
|
393
|
+
* top-level `elements` field under the same names. Read after `cleanup`, from
|
|
394
|
+
* the same page as `links` and `images`. No extra credits, also when the
|
|
395
|
+
* page comes from cache. See {@link ScrapeElements} and
|
|
396
|
+
* https://crawlbrulee.com/docs/scrape/elements.
|
|
397
|
+
*/
|
|
398
|
+
elements?: ScrapeElements;
|
|
295
399
|
/** Capture a screenshot. Omit to skip; set to a `ScreenshotRequest` to enable. */
|
|
296
400
|
screenshot?: ScreenshotRequest;
|
|
297
401
|
}
|
|
298
402
|
/**
|
|
299
403
|
* What is removed from the page before any output is built.
|
|
300
404
|
*
|
|
301
|
-
* Applies to `markdown`, `cleaned_html`, `links` and `
|
|
302
|
-
* and to the screenshot. It never applies to `raw_html` — that is always the
|
|
405
|
+
* Applies to `markdown`, `cleaned_html`, `links`, `images` and `elements` on
|
|
406
|
+
* every engine, and to the screenshot. It never applies to `raw_html` — that is always the
|
|
303
407
|
* page as it arrived, before anything was removed.
|
|
304
408
|
*/
|
|
305
409
|
interface ScrapeCleanup {
|
|
@@ -313,18 +417,18 @@ interface ScrapeCleanup {
|
|
|
313
417
|
* CSS selectors whose elements are removed before anything is captured. Use
|
|
314
418
|
* it for a banner or widget `ads_and_popups` does not recognise.
|
|
315
419
|
*
|
|
316
|
-
* At most 100 selectors, each at most 500 characters.
|
|
317
|
-
*
|
|
420
|
+
* At most 100 selectors, each at most 500 characters. A cached result is
|
|
421
|
+
* only reused for a request that removes the same selectors.
|
|
318
422
|
*/
|
|
319
423
|
exclude_selectors?: string[];
|
|
320
424
|
}
|
|
321
425
|
/**
|
|
322
426
|
* Cache settings for a scrape request. `max_age` is the only cache control.
|
|
323
427
|
* A cached result has to match the url, the screenshot setup (type, viewport,
|
|
324
|
-
* device mode), `cleanup.ads_and_popups
|
|
325
|
-
* `require_js: true` only matches browser-rendered
|
|
326
|
-
*
|
|
327
|
-
*
|
|
428
|
+
* device mode), `cleanup.ads_and_popups`, `cleanup.exclude_selectors` and
|
|
429
|
+
* `location.locale`, and `require_js: true` only matches browser-rendered
|
|
430
|
+
* results. A non-zero `actions_before` wait or scroll disables caching for that
|
|
431
|
+
* request.
|
|
328
432
|
*/
|
|
329
433
|
interface ScrapeCache {
|
|
330
434
|
/**
|
|
@@ -366,7 +470,7 @@ interface ScrapeRequest {
|
|
|
366
470
|
require_js?: boolean;
|
|
367
471
|
/**
|
|
368
472
|
* What is removed from the page before any output is built. Shapes
|
|
369
|
-
* `markdown`, `cleaned_html`, `links`, `images` and the screenshot.
|
|
473
|
+
* `markdown`, `cleaned_html`, `links`, `images`, `elements` and the screenshot.
|
|
370
474
|
*
|
|
371
475
|
* Never applies to `raw_html`, which is always the page before anything was
|
|
372
476
|
* removed.
|
|
@@ -444,7 +548,10 @@ interface ScreenshotProperties {
|
|
|
444
548
|
interface ScreenshotSlice {
|
|
445
549
|
/** 0-based row index of this slice. */
|
|
446
550
|
row_nr: number;
|
|
447
|
-
/**
|
|
551
|
+
/**
|
|
552
|
+
* Signed URL to download this slice image. Expires 24 hours after the scrape (for an async scrape, 24 hours after
|
|
553
|
+
* it was submitted). Download the image; don't keep the link.
|
|
554
|
+
*/
|
|
448
555
|
url: string;
|
|
449
556
|
type: 'slice';
|
|
450
557
|
/** Image-level properties of this slice. */
|
|
@@ -452,7 +559,10 @@ interface ScreenshotSlice {
|
|
|
452
559
|
}
|
|
453
560
|
/** Result block returned when a screenshot was requested. */
|
|
454
561
|
interface ScreenshotResult {
|
|
455
|
-
/**
|
|
562
|
+
/**
|
|
563
|
+
* Signed URL to download the full screenshot image. Expires 24 hours after the scrape (for an async scrape, 24 hours
|
|
564
|
+
* after it was submitted). Download the image; don't keep the link.
|
|
565
|
+
*/
|
|
456
566
|
url: string;
|
|
457
567
|
/** Capture mode that was used. */
|
|
458
568
|
type: ScreenshotType;
|
|
@@ -523,8 +633,9 @@ interface ScrapeMetadata {
|
|
|
523
633
|
* - `links_truncated` — the page had more than 30 000 links.
|
|
524
634
|
* - `inline_images_truncated` — the page had more than 10 000 inline images.
|
|
525
635
|
* - `raw_html_truncated` — the page body exceeded 10 000 000 characters of HTML.
|
|
526
|
-
* - `
|
|
527
|
-
*
|
|
636
|
+
* - `elements_truncated` — at least one `elements` value hit a limit: a list
|
|
637
|
+
* was cut, or a value too large to return is `null` (never cut short). See
|
|
638
|
+
* https://crawlbrulee.com/docs/scrape/elements for the limits.
|
|
528
639
|
*
|
|
529
640
|
* Unavailability — that section's extraction failed, so the field is omitted
|
|
530
641
|
* or empty while the rest of the scrape succeeded. These let you tell "the page
|
|
@@ -533,11 +644,17 @@ interface ScrapeMetadata {
|
|
|
533
644
|
* - `links_unavailable` — link extraction failed.
|
|
534
645
|
* - `inline_images_unavailable` — image extraction failed.
|
|
535
646
|
* - `metadata_unavailable` — metadata extraction failed.
|
|
647
|
+
* - `screenshot_unavailable` — a screenshot was asked for, but the page came
|
|
648
|
+
* back without one. The rest of the result is there.
|
|
536
649
|
*
|
|
537
650
|
* The page body has no such code: if it can't be extracted the scrape fails
|
|
538
651
|
* outright rather than returning a hollow `200`, and isn't billed.
|
|
652
|
+
*
|
|
653
|
+
* `metadata_truncated` is deprecated: metadata no longer has a size limit of
|
|
654
|
+
* its own, so new scrapes never send it. It stays in the union because results
|
|
655
|
+
* stored before that change can still carry it. Don't rely on it in new code.
|
|
539
656
|
*/
|
|
540
|
-
type ScrapeWarningCode = 'screenshot_truncated' | 'links_truncated' | 'inline_images_truncated' | 'raw_html_truncated' | 'metadata_truncated' | 'links_unavailable' | 'inline_images_unavailable' | 'metadata_unavailable';
|
|
657
|
+
type ScrapeWarningCode = 'screenshot_truncated' | 'links_truncated' | 'inline_images_truncated' | 'raw_html_truncated' | 'metadata_truncated' | 'elements_truncated' | 'links_unavailable' | 'inline_images_unavailable' | 'metadata_unavailable' | 'screenshot_unavailable';
|
|
541
658
|
/**
|
|
542
659
|
* Successful response from `POST /api/scrape` and `GET /api/scrape/result/:jobId`.
|
|
543
660
|
*
|
|
@@ -575,7 +692,7 @@ interface ScrapeResponse {
|
|
|
575
692
|
content_type?: string;
|
|
576
693
|
/**
|
|
577
694
|
* Extract fields that were requested but aren't supported for this
|
|
578
|
-
* content type (e.g. asking for `
|
|
695
|
+
* content type (e.g. asking for `metadata` or `elements` of a JSON file).
|
|
579
696
|
*/
|
|
580
697
|
unsupported_fields?: string[];
|
|
581
698
|
/** Page content converted to clean Markdown (when `extract.markdown`). */
|
|
@@ -591,11 +708,18 @@ interface ScrapeResponse {
|
|
|
591
708
|
images?: PageInlineImage[];
|
|
592
709
|
/** Links discovered on the page (when `extract.links`). */
|
|
593
710
|
links?: PageLink[];
|
|
711
|
+
/**
|
|
712
|
+
* The values asked for in `extract.elements`, under the same names. Every
|
|
713
|
+
* requested name is present, at every level of `fields`; a name with no
|
|
714
|
+
* match is `null` (`[]` with `all: true`). See {@link ScrapeElementValue}.
|
|
715
|
+
*/
|
|
716
|
+
elements?: Record<string, ScrapeElementValue>;
|
|
594
717
|
/**
|
|
595
718
|
* Captured screenshot (when `extract.screenshot`). In rare cases a screenshot
|
|
596
719
|
* can't be captured; when you also requested other outputs those are still
|
|
597
|
-
* returned and this field is
|
|
598
|
-
* `
|
|
720
|
+
* returned and this field is left out (so it reads back as `undefined`), with
|
|
721
|
+
* a `screenshot_unavailable` warning on `warnings` to tell this case apart —
|
|
722
|
+
* guard with `page.screenshot?.url`. A screenshot-only request
|
|
599
723
|
* that can't deliver fails instead of returning an empty response: a `422`
|
|
600
724
|
* with `unsupported_screenshot_output` when the content type can't be
|
|
601
725
|
* screenshotted, a `500` when the capture itself failed — and isn't billed.
|
|
@@ -1442,5 +1566,5 @@ interface VerifyWebhookSignatureOptions {
|
|
|
1442
1566
|
*/
|
|
1443
1567
|
declare function verifyWebhookSignature(options: VerifyWebhookSignatureOptions): Promise<WebhookVerificationResult>;
|
|
1444
1568
|
//#endregion
|
|
1445
|
-
export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TargetUnreachableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, ZeroDataRetentionNotEnabledError, isCrawlbruleeError, verifyWebhookSignature };
|
|
1569
|
+
export { AntibotBlockedError, ApiErrorDetails, ApiErrorName, ApiErrorResponse, AsyncJobStatus, AsyncJobStatusResponse, AsyncScrapeRequest, AsyncScrapeResponse, AsyncScrapeWebhook, AuthenticationError, BillingEngine, Crawlbrulee, CrawlbruleeError, type CrawlbruleeOptions, DEFAULT_BASE_URL, DEFAULT_REQUEST_TIMEOUT_MS, DEFAULT_WEBHOOK_TOLERANCE_SECONDS, ENV_API_KEY, type HttpMethod, MapBillingEngine, MapCache, MapDiscoveryCapReason, MapLinkItem, MapLocation, MapPagination, MapRequest, MapResponse, MapResponseMeta, MapTruncation, MapTypes, MapUsage, NotFoundError, PageInlineImage, PageLink, PageTooLargeError, ProxyTier, RateLimitError, RateLimitErrorDetails, type RequestOptions, ResolvedProxyTier, ResponseMeta, ScrapeCache, ScrapeCleanup, ScrapeCompleteWebhook, ScrapeCompleteWebhookData, ScrapeElementFieldSpec, ScrapeElementLeafSpec, ScrapeElementNestedFieldSpec, ScrapeElementObject, ScrapeElementOutput, ScrapeElementSpec, ScrapeElementValue, ScrapeElements, ScrapeExtract, ScrapeLocation, ScrapeMetadata, ScrapeRequest, ScrapeResponse, ScrapeWarningCode, ScrapeWebhookStatus, ScreenshotAfterAction, ScreenshotBeforeAction, ScreenshotDeviceMode, ScreenshotProperties, ScreenshotRequest, ScreenshotResult, ScreenshotScrollAction, ScreenshotSlice, ScreenshotSliceAction, ScreenshotType, ScreenshotViewport, ScreenshotViewportInfo, ScreenshotWaitAction, ServiceUnavailableError, TargetUnreachableError, TooManyRedirectsError, TransportError, Usage, UsageAllocationError, UsageAllocationErrorDetails, UsageAllocationReason, UsageLimitDetails, UsageResponse, ValidationError, type VerifyWebhookSignatureOptions, WEBHOOK_EVENT_ID_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_ROTATED_HEADER, type WaitForScrapeOptions, type WebhookSignatureSource, type WebhookVerificationFailureReason, type WebhookVerificationResult, WhoamiResponse, ZeroDataRetentionNotEnabledError, isCrawlbruleeError, verifyWebhookSignature };
|
|
1446
1570
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const DEFAULT_REQUEST_TIMEOUT_MS = 0;
|
|
|
10
10
|
/** Environment variable read by `Crawlbrulee.fromEnv()` to source the API key. */
|
|
11
11
|
const ENV_API_KEY = "CRAWLBRULEE_API_KEY";
|
|
12
12
|
/** Identifies the SDK in the `User-Agent` header. Kept in one place for easy bumping. */
|
|
13
|
-
const USER_AGENT = "@crawlbrulee/sdk/1.
|
|
13
|
+
const USER_AGENT = "@crawlbrulee/sdk/1.3.0 (node)";
|
|
14
14
|
|
|
15
15
|
//#endregion
|
|
16
16
|
//#region src/errors.ts
|