scrapfly-sdk-typescript
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseScrapfly TypeScript SDK
Scrapfly TypeScript SDK
Use the 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)中运行。
scrapfly-sdkWhen 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-sdkbash
npm install scrapfly-sdkDeno / 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 渲染
ScrapeConfigtypescript
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
}),
);ScrapeConfigtypescript
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
重要说明
- is required for
render_js: true,rendering_wait, and screenshotswait_for_selector - is recommended alongside
proxy_pool: 'public_residential_pool'unblocker: true - is the current name for the anti-bot bypass;
unblockeris the deprecated alias and keeps working forever. Only the name changed; the SDK sendsaspon the wire either way. Pass one, not both; an explicitly suppliedaspwins overaspunblocker - Use for clean, LLM-friendly content
format: 'markdown' - The API key is read from the constructor option (pass
key)process.env.SCRAPFLY_API_KEY - Read the page body via
result.result.content
- 使用 、
rendering_wait和屏幕截图功能时,必须设置wait_for_selectorrender_js: true - 建议将 与
proxy_pool: 'public_residential_pool'搭配使用unblocker: true - 是当前用于反机器人绕过的名称;
unblocker是已弃用的别名,但会永久保持可用。仅名称发生变化;无论使用哪一个,SDK 在网络上发送的都是asp。只能传入其中一个,不要同时传入;如果显式提供asp,则优先于aspunblocker - 使用 可获取简洁、适合 LLM 使用的内容
format: 'markdown' - API 密钥通过 构造函数选项读取(传入
key)process.env.SCRAPFLY_API_KEY - 通过 读取页面正文
result.result.content