> 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/shi-pin-yu-she-jiao-mei-ti/youtube/youtube-scraping-guide-for-ai.md).

# 用于 AI 的抓取指南

本指南将带你了解如何使用 网页爬虫API 的专用来源来收集和筛选 YouTube 数据，用于 AI 训练。 [**网页爬虫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`的响应中，视频 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：获取视频元数据&#x20;

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

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

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

{% hint style="success" %}
对于 `parse` 参数， `必须设置为` 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** 图标以开启字幕/说明文字。&#x20;

如果你想选择特定语言的字幕，请找到 **Settings** 图标旁边的 **CC** 图标，点击它并进入 **"Subtitles/CC"** 选项。在那里你会找到 **Auto-translate**，点击后会进入语言列表。

如果字幕可用，你可以这样获取：

```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:

```
GET https://developers.oxylabs.io/api-targets/cn/shi-pin-yu-she-jiao-mei-ti/youtube/youtube-scraping-guide-for-ai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
