scrapfly-sdk-typescript

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Scrapfly TypeScript SDK

Scrapfly TypeScript SDK

Use the
scrapfly-sdk
package to scrape web pages from TypeScript/JavaScript with proxy rotation, anti-bot bypass (Unblocker), JavaScript rendering, screenshots, and AI extraction. Runs on Node.js, Deno, Bun, and edge runtimes (Cloudflare Workers, AWS Lambda).
使用
scrapfly-sdk
包从 TypeScript/JavaScript 抓取网页,支持代理轮换、反机器人绕过(Unblocker)、JavaScript 渲染、屏幕截图和 AI 数据提取。可在 Node.js、Deno、Bun 及边缘运行时(Cloudflare Workers、AWS Lambda)中运行。

When to use

使用场景

  • Scraping web pages (HTML, JSON, text, markdown) from a TS/JS codebase
  • Bypassing anti-bot protections (Cloudflare, DataDome, PerimeterX, Kasada)
  • Rendering JavaScript-heavy pages with a headless browser
  • Capturing screenshots or extracting structured data
  • Concurrent scraping of many URLs
  • 从 TS/JS 代码库中抓取网页(HTML、JSON、文本、Markdown)
  • 绕过反机器人保护(Cloudflare、DataDome、PerimeterX、Kasada)
  • 使用无头浏览器渲染大量 JavaScript 的页面
  • 截取屏幕截图或提取结构化数据
  • 并发抓取多个 URL

Setup

设置

bash
npm install scrapfly-sdk
bash
npm install scrapfly-sdk

Deno / JSR: import from 'jsr:@scrapfly/scrapfly-sdk'

Deno / JSR: import from 'jsr:@scrapfly/scrapfly-sdk'

export SCRAPFLY_API_KEY="your-api-key"

Get your API key at [scrapfly.io](https://scrapfly.io).
export SCRAPFLY_API_KEY="your-api-key"

在 [scrapfly.io](https://scrapfly.io) 获取 API 密钥。

Basic scrape

基本抓取

typescript
import { ScrapflyClient, ScrapeConfig } from 'scrapfly-sdk';

const client = new ScrapflyClient({ key: process.env.SCRAPFLY_API_KEY });

const result = await client.scrape(
    new ScrapeConfig({ url: 'https://web-scraping.dev/products' }),
);

console.log(result.result.content);
typescript
import { ScrapflyClient, ScrapeConfig } from 'scrapfly-sdk';

const client = new ScrapflyClient({ key: process.env.SCRAPFLY_API_KEY });

const result = await client.scrape(
    new ScrapeConfig({ url: 'https://web-scraping.dev/products' }),
);

console.log(result.result.content);

Anti-bot bypass, geo-targeting, JS rendering

反机器人绕过、地理位置定向与 JS 渲染

ScrapeConfig
uses snake_case option keys, matching the API.
typescript
const result = await client.scrape(
    new ScrapeConfig({
        url: 'https://web-scraping.dev/products',
        unblocker: true,                    // bypass anti-bot
        country: 'us',                      // proxy country (ISO 3166-1 alpha-2)
        proxy_pool: 'public_residential_pool',
        render_js: true,                    // headless browser rendering
        wait_for_selector: 'div.product',   // requires render_js
        rendering_wait: 5000,               // ms, requires render_js
        format: 'markdown',                 // raw | clean_html | json | markdown | text
    }),
);
ScrapeConfig
使用与 API 一致的 snake_case 选项键。
typescript
const result = await client.scrape(
    new ScrapeConfig({
        url: 'https://web-scraping.dev/products',
        unblocker: true,                    // bypass anti-bot
        country: 'us',                      // proxy country (ISO 3166-1 alpha-2)
        proxy_pool: 'public_residential_pool',
        render_js: true,                    // headless browser rendering
        wait_for_selector: 'div.product',   // requires render_js
        rendering_wait: 5000,               // ms, requires render_js
        format: 'markdown',                 // raw | clean_html | json | markdown | text
    }),
);

Screenshots and extraction

屏幕截图与数据提取

typescript
import { ScreenshotConfig, ExtractionConfig } from 'scrapfly-sdk';

const shot = await client.screenshot(
    new ScreenshotConfig({ url: 'https://web-scraping.dev/products' }),
);
// shot.image is the binary image data

const html = (await client.scrape(
    new ScrapeConfig({ url: 'https://web-scraping.dev/reviews' }),
)).result.content;

const data = await client.extract(
    new ExtractionConfig({
        body: html,
        content_type: 'text/html',
        extraction_prompt: 'extract the reviews as JSON with fields: review, date, rating, author',
    }),
);
console.log(data.data);
typescript
import { ScreenshotConfig, ExtractionConfig } from 'scrapfly-sdk';

const shot = await client.screenshot(
    new ScreenshotConfig({ url: 'https://web-scraping.dev/products' }),
);
// shot.image is the binary image data

const html = (await client.scrape(
    new ScrapeConfig({ url: 'https://web-scraping.dev/reviews' }),
)).result.content;

const data = await client.extract(
    new ExtractionConfig({
        body: html,
        content_type: 'text/html',
        extraction_prompt: 'extract the reviews as JSON with fields: review, date, rating, author',
    }),
);
console.log(data.data);

Concurrent scraping

并发抓取

typescript
const configs = [
    new ScrapeConfig({ url: 'https://web-scraping.dev/products?page=1' }),
    new ScrapeConfig({ url: 'https://web-scraping.dev/products?page=2' }),
];

for await (const result of client.concurrentScrape(configs)) {
    console.log(result.result.content.slice(0, 100));
}
typescript
const configs = [
    new ScrapeConfig({ url: 'https://web-scraping.dev/products?page=1' }),
    new ScrapeConfig({ url: 'https://web-scraping.dev/products?page=2' }),
];

for await (const result of client.concurrentScrape(configs)) {
    console.log(result.result.content.slice(0, 100));
}

Important notes

重要说明

  • render_js: true
    is required for
    rendering_wait
    ,
    wait_for_selector
    , and screenshots
  • proxy_pool: 'public_residential_pool'
    is recommended alongside
    unblocker: true
  • unblocker
    is the current name for the anti-bot bypass;
    asp
    is the deprecated alias and keeps working forever. Only the name changed; the SDK sends
    asp
    on the wire either way. Pass one, not both; an explicitly supplied
    asp
    wins over
    unblocker
  • Use
    format: 'markdown'
    for clean, LLM-friendly content
  • The API key is read from the
    key
    constructor option (pass
    process.env.SCRAPFLY_API_KEY
    )
  • Read the page body via
    result.result.content
  • 使用
    rendering_wait
    、
    wait_for_selector
    和屏幕截图功能时,必须设置
    render_js: true
  • 建议将
    proxy_pool: 'public_residential_pool'
    与
    unblocker: true
    搭配使用
  • unblocker
    是当前用于反机器人绕过的名称;
    asp
    是已弃用的别名,但会永久保持可用。仅名称发生变化;无论使用哪一个,SDK 在网络上发送的都是
    asp
    。只能传入其中一个,不要同时传入;如果显式提供
    asp
    ,则优先于
    unblocker
  • 使用
    format: 'markdown'
    可获取简洁、适合 LLM 使用的内容
  • API 密钥通过
    key
    构造函数选项读取(传入
    process.env.SCRAPFLY_API_KEY
    )
  • 通过
    result.result.content
    读取页面正文