> ## 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/cp/start)（每月含 5,000 个免费积分）
* 已安装 cURL、Python 3 或 Node.js 18+

<Steps>
  <Step title="获取你的 API 密钥">
    访问你 Bright Data 账户中的 [用户设置页面](https://www.bright.cn/cp/setting/users) 并复制你的 API 密钥。

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

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

  <Step title="发送请求">
    我们将使用**帖子 — 按 URL 收集**端点进行同步请求。将 `YOUR_API_KEY` 替换为你的实际密钥：

    <CodeGroup>
      ```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/1asdf12/how_do_i_start_learning_python/"}]'
      ```

      ```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/1asdf12/how_do_i_start_learning_python/"}],
      )

      print(response.json())
      ```

      ```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/1asdf12/how_do_i_start_learning_python/" }
          ]),
        }
      );

      const data = await response.json();
      console.log(data);
      ```
    </CodeGroup>

    你应该看到 `200` 状态码。这需要 10 到 30 秒。
  </Step>

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

    ```json theme={null}
    [
      {
        "post_id": "1asdf12",
        "url": "https://www.reddit.com/r/learnpython/comments/1asdf12/how_do_i_start_learning_python/",
        "user_posted": "example_user",
        "title": "How do I start learning Python?",
        "description": "I'm a complete beginner...",
        "num_upvotes": 1240,
        "num_comments": 86,
        "date_posted": "2025-03-14T18:22:00Z",
        "community_name": "learnpython",
        "community_url": "https://www.reddit.com/r/learnpython",
        "community_members_num": 1120000,
        "tag": "Tutorial"
      }
    ]
    ```

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

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

## 常见问题

<Accordion title="我可以在一次请求中爬取多个帖子吗？">
  可以。在输入数组中添加更多对象。同步请求最多支持 20 个 URL。对于更大的批次或按关键词或子版块发现，请使用 [异步 `/trigger` 端点](/cn/products/scrapers/scrapers-library/async-requests)。

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

<Accordion title="我可以爬取帖子中的评论吗？">
  可以，使用单独的评论数据集。使用数据集 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/1asdf12/"}]'
  ```

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

<Accordion title="收到 401 或 403 错误？">
  确认你的 API 密钥正确且未过期。从 [账户设置](https://www.bright.cn/cp/setting/users) 生成新密钥。详见 [认证指南](/cn/api-reference/authentication)。
</Accordion>

<Accordion title="请求超时？">
  同步请求有 1 分钟超时限制。如果请求超过此限制，它会自动切换到异步模式并返回 `snapshot_id`。对于大批量请求，请使用 [异步工作流](/cn/products/scrapers/scrapers-library/async-requests)。
</Accordion>

<Accordion title="响应数据为空或不完整？">
  确认 Reddit 帖子 URL 可公开访问且格式正确。URL 应遵循 `https://www.reddit.com/r/{subreddit}/comments/{post_id}/{slug}/` 模式。私有子版块和已删除的帖子无法被爬取。
</Accordion>

## 后续步骤

<CardGroup cols={3}>
  <Card title="发送第一个请求" icon="bolt" href="/cn/products/scrapers/reddit/send-first-request">
    使用 cURL、Python 和 Node.js 探索每个端点的完整示例。
  </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>
