跳转到主要内容

Documentation Index

Fetch the complete documentation index at: https://docs.brightdata.com/llms.txt

Use this file to discover all available pages before exploring further.

本指南介绍如何安装 Bright Data JavaScript SDK 并在 Node.js 中抓取 URL、运行搜索、调用平台专用 scrapers(LinkedIn、Amazon、Instagram 等)、连接 Browser API 与 Scraper Studio。

安装包

打开终端并运行:
npm install @brightdata/sdk
在你的代码文件中,导入包并发起第一次请求:
import { bdclient } from '@brightdata/sdk';

const client = new bdclient({ apiKey: 'YOUR_API_KEY' });

const html = await client.scrapeUrl('https://example.com');
const google = await client.search.google('pizza restaurants');

console.log(google);
await client.close();

发起抓取和网络搜索

import { bdclient } from '@brightdata/sdk';

const client = new bdclient({ apiKey: 'YOUR_API_KEY' });

const google = await client.search.google('best laptops 2026');
const bing = await client.search.bing('python tutorial');
const yandex = await client.search.yandex('AI news');

console.log({ google, bing, yandex });
await client.close();

平台 scrapers 与数据集

按 URL 采集主流平台数据,或按参数发现内容。
import { bdclient } from '@brightdata/sdk';

const client = new bdclient({ apiKey: 'YOUR_API_KEY' });

// 按 URL 采集
const profiles = await client.scrape.linkedin.collectProfiles([
    'https://www.linkedin.com/in/satyanadella/',
]);
const companies = await client.scrape.linkedin.collectCompanies([
    'https://www.linkedin.com/company/bright-data',
]);
const jobs = await client.scrape.linkedin.collectJobs([
    'https://www.linkedin.com/jobs/view/123456',
]);
const posts = await client.scrape.linkedin.collectPosts([
    'https://www.linkedin.com/feed/update/urn:li:activity:123',
]);

// 按参数发现
const byKeyword = await client.scrape.linkedin.discoverJobs(
    [{ keyword: 'python developer', location: 'New York', remote: true }],
    {}
);
const byName = await client.scrape.linkedin.discoverProfiles(
    [{ firstName: 'John', lastName: 'Doe' }],
    {}
);
const compPosts = await client.scrape.linkedin.discoverCompanyPosts(
    [{ url: 'https://www.linkedin.com/company/bright-data' }],
    {}
);
const userPosts = await client.scrape.linkedin.discoverUserPosts(
    [{ url: 'https://www.linkedin.com/in/satyanadella' }],
    {}
);

Discover API

一次调用即可搜索网页并返回 AI 排序的结果,或者手动触发与轮询。
// 一次调用:搜索、轮询并返回 AI 排序结果
const result = await client.discover('AI trends 2026', {
    intent: 'latest technology developments',
});
// result.data 形如 [{ title, link, description, relevance_score }]

// 手动:触发、等待并获取
const job = await client.discoverTrigger('SaaS pricing', {
    intent: 'competitor pricing strategies',
});
await job.wait({ timeout: 60_000 });
const data = await job.fetch();

Scraper Studio

通过 SDK 运行您的自定义 Scraper Studio 采集器。
// 一次调用即可运行并获取结果
const results = await client.scraperStudio.run('c_your_collector_id', {
    input: { url: 'https://example.com/product/1' },
});
// results 形如 [{ input, data, error, responseId, elapsedMs }]

// 手动:触发、等待并获取
const job = await client.scraperStudio.trigger('c_your_collector_id', {
    url: 'https://example.com/product/1',
});
const data = await job.waitAndFetch();

// 查询状态
const status = await client.scraperStudio.status('j_abc123');
// status.status 取值为 'queued' | 'running' | 'done' | 'failed'

Browser API

将 Playwright 连接到 Bright Data 的云端浏览器。
import { bdclient } from '@brightdata/sdk';
import { chromium } from 'playwright';

const client = new bdclient({
    apiKey: 'YOUR_API_KEY',
    browserUsername: 'brd-customer-xxxx-zone-scraping_browser1',
    browserPassword: 'YOUR_ZONE_PASSWORD',
});

const browser = await chromium.connectOverCDP(client.browser.getConnectUrl());
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
await client.close();

Datasets API

查询并下载 Bright Data 126+ 数据集中的任意一项。
const ds = client.datasets;

// 查询,然后下载
const snapshotId = await ds.instagramProfiles.query(
    { url: 'https://www.instagram.com/natgeo/' },
    { records_limit: 10 }
);
const rows = await ds.instagramProfiles.download(snapshotId);

// 整个数据集目录使用相同模式
await ds.amazonProducts.query({ url: 'https://amazon.com/dp/B123' });
await ds.linkedinProfiles.query({ url: 'https://linkedin.com/in/johndoe' });
await ds.imdbMovies.query({}, { records_limit: 50 });
await ds.redditPosts.query({ subreddit: 'technology' });
await ds.glassdoorReviews.query({ company: 'Bright Data' });

// 获取字段元数据
const meta = await ds.instagramProfiles.getMetadata();

客户端与方法参数

参数类型说明默认值
apiKeystring你的 API key(也可通过 BRIGHTDATA_API_KEY 环境变量提供).
autoCreateZonesboolean当 zone 不存在时自动创建true
webUnlockerZonestring自定义 Web Unlocker zone 名称.
serpZonestring自定义 SERP zone 名称.
browserUsernamestringBrowser API 用户名(也可通过 BRIGHTDATA_BROWSERAPI_USERNAME 环境变量提供).
browserPasswordstringBrowser API 密码(也可通过 BRIGHTDATA_BROWSERAPI_PASSWORD 环境变量提供).
logLevelstring日志级别'INFO'
structuredLoggingboolean使用结构化 JSON 日志true
verboseboolean启用详细日志false
const client = new bdclient({
    apiKey: 'YOUR_API_KEY',
    autoCreateZones: true,
    webUnlockerZone: 'unlocker_zone1',
    serpZone: 'serp_zone1',
    browserUsername: 'brd-customer-xxxx-zone-scraping_browser1',
    browserPassword: 'YOUR_ZONE_PASSWORD',
    logLevel: 'INFO',
    structuredLogging: true,
    verbose: false,
});
使用 Web Unlocker API 抓取单个 URL 或 URL 列表。
参数类型说明默认值
urlstring | string[]单个 URL 字符串或 URL 数组.
options.zonestringZone 标识(如为 null 则自动配置).
options.format'json' | 'raw'响应格式'raw'
options.methodstringHTTP 方法'GET'
options.countrystring两位字母国家代码''
options.dataFormat'markdown' | 'screenshot' | 'html'返回内容格式'html'
options.concurrencynumber最大并发 worker 数10
options.timeoutnumber (ms)请求超时30000
参数类型说明默认值
contentany要保存的内容.
options.filenamestring输出文件名(如为 null 则自动生成).
options.format'json' | 'csv' | 'txt'文件格式.

错误处理

在 Client 中启用 VERBOSE 可获取高级日志(见客户端参数)。使用 listZones() 函数可查询可用 zones。
创建一个 Bright Data 账户并复制您的 API key。前往 账户设置,并确保您的 API key 拥有 管理员权限

资源

GitHub

访问 Bright Data SDK 的 GitHub 仓库