> For the complete documentation index, see [llms.txt](https://developers.oxylabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.oxylabs.io/api-targets/cn/video-and-social-media/youtube/youtube-scraping-guide-for-ai.md).

# AI 抓取指南

了解如何使用网页爬虫API 获取用于 AI 训练的 YouTube 数据。进一步了解从搜索视频到创建数据库的完整工作流程。

本指南将带你了解使用 [**网页爬虫API 的专用来源**](https://oxylabs.io/products/scraper-api/web/youtube): `youtube_search`, `youtube_video_trainability`, `youtube_metadata`, `youtube_download`, `youtube_subtitles`.

## 步骤 1：搜索视频

先搜索与你感兴趣的主题相关的视频。

### 基础搜索

如需快速搜索，最多返回 20 个结果：

```json
{
  "source": "youtube_search",
  "query": "your search term"
}
```

### 扩展搜索

如需更全面的结果（最多 700 个结果）：

```json
{
  "source": "youtube_search_max",
  "query": "your search term"
}
```

### 使用过滤条件搜索

使用过滤条件优化搜索：

```json
{
  "source": "youtube_search",
  "query": "your search term",
  "type": "video",
  "duration": "4-20",
  "upload_date": "this_month",
  "sort_by": "view_count",
  "hd": true
}
```

{% hint style="info" %}
使用合适的过滤条件，根据你的具体需求缩小结果范围。可选项包括内容类型（视频、频道、播放列表）、时长、上传日期和质量设置。
{% endhint %}

## 步骤 2：从搜索结果中提取视频 ID

在收到搜索结果后，提取 **视频 ID** 用于进一步处理。在来自 `youtube_search` 或 `youtube_search_max`的响应中，video ID 直接位于 `videoId` 字段中，如下面的示例响应片段所示：

```json
{
    "results": [
        {
            "content": [
                {
                    "videoId": "LK9XuImr8Xg",  // This is the video ID you need
                    "thumbnail": {
                        "thumbnails": [
                            {
                                "url": "https://i.ytimg.com/vi/LK9XuImr8Xg/hq720_2.jpg?sqp=-oaymwE2COgCEMoBSFXyq4qpAygIARUAAIhCGABwAcABBvABAfgBtgiAAoAPigIMCAAQARhaIGUoLTAP&rs=AOn4CLDTvqEgoE2ZNfnn3EalF2ujcthVNw",
                                "width": 360,
                                "height": 202
                            }
                        ]
                    },
                    "title": {
                        // title details
                    }
                }
            ]
        }
    ]
}
```

将这些视频 ID 提取到列表中，以便在后续 API 调用中使用。

## 步骤 3：检查 AI 训练资格

在下载或将视频用于 AI 训练之前，先检查其资格：

```json
{
  "source": "youtube_video_trainability",
  "video_id": "rFNDylrjn_w"
}
```

响应会指示该视频是否可用于 AI 训练：

* `["all"]` - 允许所有方训练
* `["none"]` - 不允许任何方训练
* `["party1", "party2", ...]` - 仅允许特定方训练

## 步骤 4：获取视频元数据

收集更多视频信息，以进一步评估其质量和相关性：

```json
{
  "source": "youtube_metadata",
  "query": "VIDEO_ID",
  "parse": true
}
```

响应将包含观看次数、评论、评分和其他指标等元数据，这些信息可帮助你评估内容质量。

{% hint style="success" %}
该 `解析` 参数必须设置为 `true` ，用于元数据来源。
{% endhint %}

## 步骤 5：检索所选视频的内容

在根据其资格和元数据识别出高质量、可训练的视频后，即可进行内容检索。这可以分两个并行步骤完成：

### 5.1 下载视频/音频内容

```json
{
  "source": "youtube_download",
  "query": "VIDEO_ID",
  "storage_type": "s3",
  "storage_url": "s3://your-bucket/your-folder/"
}
```

下载的其他选项：

```json
{
  "source": "youtube_download",
  "query": "VIDEO_ID",
  "storage_type": "s3",
  "storage_url": "s3://your-bucket/your-folder/",
  "context": [
    {
      "key": "download_type",
      "value": "video"
    },
    {
      "key": "video_quality",
      "value": "1080"
    }
  ]
}
```

{% hint style="info" %}
此来源仅可通过异步 [**Push-Pull 集成**](/products/cn/web-scraper-api/integration-methods/push-pull.md) 和 [**云存储**](/products/cn/web-scraper-api/features/result-processing-and-storage/cloud-storage.md) 功能。
{% endhint %}

**注意：**

* 视频长度最长可达 3 小时
* 默认分辨率为 720p（可自定义）
* 你可以指定仅音频、仅视频或两者都要

### 5.2 检索视频字幕

#### **手动检查视频是否有字幕：**

在 YouTube 中，点击 **CC** 视频下方的图标可打开字幕/隐藏式字幕。

如果你想选择特定语言的字幕，请找到 **设置** 旁边的 **CC** 图标，点击它并进入 **"字幕/CC"** 选项。你会在那里找到 **自动翻译**，点击它后会进入语言列表。

如果字幕可用，你可以通过以下方式检索：

```json
{
  "source": "youtube_subtitles",
  "query": "VIDEO_ID",
  "context": [
    {
      "key": "language_code",
      "value": "en"
    }
  ]
}
```

对于手动创建字幕的视频，请指定：

```json
{
    "source": "youtube_subtitles",
    "query": "VIDEO_ID",
    "context": [
        {
            "key": "language_code",
            "value": "en"
        },
        {
            "key": "subtitle_origin",
            "value": "uploader_provided"
        }
    ]
}
```

## 批量处理

如需高效处理多个视频，请使用批量端点：

```json
{
  "source": "youtube_video_trainability",
  "query": ["VIDEO_ID_1", "VIDEO_ID_2", "VIDEO_ID_3"]
}
```

## 最佳实践

1. 遵循以下发现工作流： **搜索 → 可训练性 → 元数据 → 内容** 以最大化效率
2. 在处理单个视频之前先缩小搜索结果范围
3. 在将内容用于 AI 之前，始终验证可训练性
4. 检查 [**响应代码**](/products/cn/web-scraper-api/response-codes.md) 并为失败的请求实现重试


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.oxylabs.io/api-targets/cn/video-and-social-media/youtube/youtube-scraping-guide-for-ai.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
