> ## 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.

# Reddit 爬虫快速开始

> 在 5 分钟内设置 Bright Data Reddit 爬虫 API 并收集你的第一个帖子。

本教程向你展示如何使用 Bright Data Reddit 爬虫 API 爬取 Reddit 帖子并获得结构化 JSON 数据。

## 前提条件

* 一个 [Bright Data 账户](https://www.bright.cn/products/web-scraper/reddit?hs_signup=1\&utm_source=docs)（每月含 5,000 个免费积分）
* 已安装 cURL、Python 3.9+ 或 Node.js 18+

<Steps>
  <Step title="获取你的 API 密钥">
    访问你 Bright Data 账户中的 [用户设置页面](https://www.bright.cn/cp/setting/users) 并创建一个 API 密钥，或使用已有的密钥。

    如果你还没有账户，[在 brightdata.com 注册](https://www.bright.cn/products/web-scraper/reddit?hs_signup=1\&utm_source=docs)。新账户每月可获得 5,000 个免费积分，无需信用卡。请参阅[免费套餐](/cn/general/account/billing-and-pricing/free-tier)。

    <Warning>
      你的 API 密钥仅在创建时显示一次。请复制并安全地存储。
    </Warning>
  </Step>

  <Step title="发送请求">
    我们将使用**帖子 — 按 URL 收集**端点。最快的方式是使用 Bright Data Python SDK（`pip install brightdata-sdk`），它会替您等待结果。其他选项卡通过同步请求直接调用 API。将 `YOUR_API_KEY` 替换为你的实际密钥：

    <CodeGroup>
      ```python Python SDK theme={null}
      # pip install brightdata-sdk
      from brightdata import SyncBrightDataClient

      # The Scraper API uses no zones, so skip the SDK's zone setup
      with SyncBrightDataClient(token="YOUR_API_KEY", auto_create_zones=False) as client:
          result = client.scrape.reddit.posts(
              url="https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/"
          )
          print(result.data)
      ```

      ```bash cURL theme={null}
      curl -X POST \
        "https://api.brightdata.com/datasets/v3/scrape?dataset_id=gd_lvz8ah06191smkebj4&format=json" \
        -H "Authorization: Bearer YOUR_API_KEY" \
        -H "Content-Type: application/json" \
        -d '[{"url": "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/"}]'
      ```

      ```python Python theme={null}
      import requests

      response = requests.post(
          "https://api.brightdata.com/datasets/v3/scrape",
          params={"dataset_id": "gd_lvz8ah06191smkebj4", "format": "json"},
          headers={
              "Authorization": "Bearer YOUR_API_KEY",
              "Content-Type": "application/json",
          },
          json=[{"url": "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/"}],
      )

      data = response.json()
      if response.status_code == 202:
          print(f"Job running. Snapshot ID: {data['snapshot_id']}")
      else:
          print(data)
      ```

      ```javascript Node.js theme={null}
      const response = await fetch(
        "https://api.brightdata.com/datasets/v3/scrape?dataset_id=gd_lvz8ah06191smkebj4&format=json",
        {
          method: "POST",
          headers: {
            "Authorization": "Bearer YOUR_API_KEY",
            "Content-Type": "application/json",
          },
          body: JSON.stringify([
            { url: "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/" }
          ]),
        }
      );

      const data = await response.json();
      if (response.status === 202) {
        console.log(`Job running. Snapshot ID: ${data.snapshot_id}`);
      } else {
        console.log(data);
      }
      ```
    </CodeGroup>

    SDK 会输出 `result.data` 中返回的记录。它最多等待 240 秒获取结果，因此您无需自行处理 `202`。使用 cURL、Python 或 Node.js 时，您应该看到 `200` 状态码和一个 JSON 数组。`202` 表示抓取需要超过 1 分钟，请参阅下方说明。处理时间取决于目标网站状况和爬虫负载。

    <Note>
      同步请求最多等待 1 分钟。如果 Bright Data Reddit Scraper API 需要更长时间，响应为 HTTP `202`，返回 `snapshot_id` 而不是帖子数据，采集将继续以异步方式进行。请通过[异步工作流](/cn/products/scrapers/scrapers-library/async-requests)获取结果，或使用 [Webhook 投递](/cn/products/scrapers/scrapers-library/data-delivery)接收结果。
    </Note>
  </Step>

  <Step title="查看响应">
    Bright Data Reddit 爬虫 API 返回一个包含结构化帖子数据的 JSON 数组：

    ```json theme={null}
    [
      {
        "post_id": "t3_1w8chn8",
        "url": "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/",
        "user_posted": "xq_***x",
        "title": "found a for loop and can't find an explanation. Can someone explain, please?",
        "description": "Hey there, I just started the Project Euler Archive and started with the classic \"Find the sum of all the multiples of 3 or 5 below 1000.\" And when I wanted...",
        "num_upvotes": 8,
        "num_comments": 14,
        "date_posted": "2026-09-05T21:03:33.625Z",
        "community_name": "learnpython",
        "community_url": "https://www.reddit.com/r/learnpython/"
      }
    ]
    ```

    每个帖子对象包括帖子详情、社区统计、互动指标和附带的媒体。查看 [完整响应模式](/cn/api-reference/scrapers/social-media-apis/reddit#posts-api)。
  </Step>
</Steps>

你已经使用 Bright Data Reddit 爬虫 API 成功爬取了第一个 Reddit 帖子。

## 常见问题

### 我可以在一次请求中爬取多个帖子吗？

可以。在输入数组中添加更多对象。Bright Data 建议每个同步请求不超过 20 个 URL，这是最佳实践，而非硬性限制。对于更大的批次或按关键词或子版块发现，请使用 [异步 `/trigger` 端点](/cn/products/scrapers/scrapers-library/async-requests)。

```json theme={null}
[
  {"url": "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/"},
  {"url": "https://www.reddit.com/r/python/comments/1bsdf34/"},
  {"url": "https://www.reddit.com/r/programming/comments/1csdf56/"}
]
```

### 我可以爬取帖子中的评论吗？

可以，使用单独的评论数据集。使用数据集 ID `gd_lvzdpsdlw09j6t702` 并传入帖子 URL：

```bash theme={null}
curl -X POST \
  "https://api.brightdata.com/datasets/v3/scrape?dataset_id=gd_lvzdpsdlw09j6t702&format=json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '[{"url": "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/"}]'
```

你也可以传入 `days_back` 将结果限制为最近 N 天内发布的评论。

### 收到 401 或 403 错误？

确认你的 API 密钥正确且未过期。从 [账户设置](https://www.bright.cn/cp/setting/users) 生成新密钥。详见 [认证指南](/cn/api-reference/authentication)。

### 请求超时？

同步请求有 1 分钟超时限制。如果请求超过此限制，API 返回 HTTP `202` 和 `snapshot_id`，并以异步方式完成任务。对于大批量请求，请使用 [异步工作流](/cn/products/scrapers/scrapers-library/async-requests)。

### 响应数据为空或不完整？

确认 Reddit 帖子 URL 可公开访问且格式正确。URL 应遵循 `https://www.reddit.com/r/{subreddit}/comments/{post_id}/{slug}/` 模式。私有子版块和已删除的帖子无法被爬取。

## 必须直接调用 API 吗？

不必。Bright Data 的 SDK、CLI 和编程代理工具调用的是同一个 Scraper API，您可以选择最适合自己技术栈的方式开始。

| 客户端 | 安装 | 第一次调用 |
| :- | :- | :- |
| **Python 和 Node.js SDK** | `pip install brightdata-sdk` 或 `npm install @brightdata/sdk` | `client.scrape.reddit.posts(url="https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/")`；详见 [Python SDK](/cn/api-reference/SDK) 和 [JavaScript SDK](/cn/api-reference/SDK-JS) |
| **CLI** | `npm install -g @brightdata/cli`，然后 `bdata login` | `bdata pipelines reddit_posts "https://www.reddit.com/r/learnpython/comments/1w8chn8/found_a_for_loop_and_cant_find_an_explanation_can/" --pretty`；详见 [CLI 命令](/cn/products/cli/commands) |
| **编程代理** | `npx skills add brightdata/skills` 安装技能；[Bright Data MCP](/cn/products/mcp-server/overview) 服务器将爬虫作为工具暴露 | 详见[编程代理快速开始](/cn/quickstart-coding-agent) |

## 后续步骤

<CardGroup cols={3}>
  <Card title="端点" icon="bolt" href="/cn/products/scrapers/reddit/send-first-request">
    每个端点一个请求，附实际抓取的响应。
  </Card>

  <Card title="异步批量请求" icon="layer-group" href="/cn/products/scrapers/scrapers-library/async-requests">
    在单个批量任务中爬取数百个帖子或运行关键词发现。
  </Card>

  <Card title="API 参考" icon="code" href="/cn/api-reference/scrapers/social-media-apis/reddit">
    端点规范、参数和响应模式。
  </Card>
</CardGroup>
