logo
languageENdown
menu

How to Use a YouTube Scraper API with Python

star

Use a YouTube scraper API with Python to discover a template, run a cloud task, poll export status, and validate structured channel data before automation.

14 min read

To use a YouTube scraper API with Python, choose a cloud-capable scraping template, read its live input schema, authenticate your request, create the task once, and poll the export until structured rows are ready. Your Python code sends the permitted YouTube channel, search, or video URL, preserves the returned task ID, and writes the finished dataset to a CSV file, spreadsheet, database, or analysis workflow.

A YouTube scraper API is usually asynchronous. A successful task-creation response means collection has started; it does not prove that the export is ready. Begin with a small public target, validate the returned fields, and use bounded polling before scaling the workflow. This guide demonstrates that lifecycle with the Octoparse API and explains how the same cloud templates can also be controlled through Octoparse MCP or CLI.

Tested evidence: A first-party Octoparse API run on August 14, 2026 returned 50 populated video records in about four minutes. A new live MCP run started on August 30, and a snapshot captured on August 31 followed the current official workflow and returned 199 observed rows with five populated preview records. The task still reported running, so 199 is an observed snapshot rather than a guaranteed final total.

How to Use a YouTube Scraper API with Python

The shortest reliable path is to discover the live template, build parameters from its schema, authenticate, create one cloud task, and poll the export with a fixed time limit. The Python client should preserve the task ID before any retry and should treat an accepted task and a populated export as different states.

  1. Install requests and load the API key from an environment variable.
  2. Discover youtube-channel-scraper-free and read its current schema.
  3. Send the permitted channel Videos page URL in youTube_Account_URLs.
  4. Create the task once and save the returned task ID.
  5. Poll exportData until a terminal status is returned, then validate the rows before using them.

Prepare the Python Environment

Before you write the executor, prepare:

  • A recent Python 3 release
  • The requests package
  • An Octoparse API key
  • A stable external user ID
  • Access to a cloud-executable YouTube template
  • A public YouTube channel handle you are permitted to process

Install the Python dependency:

python -m pip install requests

Store secrets outside your source code:

# PowerShell
$env:OCTOPARSE_API_KEY="your-api-key"
$env:OCTOPARSE_EXTERNAL_USER_ID="your-stable-user-id"

Use the same external user ID when you need to find or reconcile tasks created for the same application user. Do not generate a random value for every retry.

Octoparse documents the new-task sequence as searchTemplates → executeTask → exportData. The Octoparse API workflow also specifies the base URL, authentication headers, schema rules, and export statuses used below.

Octoparse API workflow overview

Step 1: Find the Exact YouTube Template

YouTube Channel Scraper (Free) is the URL-based template used in the populated API test. Its live discovery result identified template ID 1813, Cloud and Local execution modes, one required URL-array input, and ten output fields.

https://www.octoparse.com/template/youtube-channel-scraper-free

YouTube Channel Scraper template page in Octoparse

Start with searchTemplates. Search for the exact slug rather than a broad keyword, then match the returned template by slug. Do not select the first item simply because it mentions YouTube.

import os
import requests

BASE_URL = "https://openapi.octoparse.com"
TARGET_SLUG = "youtube-channel-scraper-free"
API_KEY = os.environ["OCTOPARSE_API_KEY"]

response = requests.get(
    f"{BASE_URL}/api/agentTools/searchTemplates",
    params={"slug": TARGET_SLUG, "page": 1, "limit": 1},
    headers={"x-api-key": API_KEY},
    timeout=30,
)
response.raise_for_status()
search_payload = response.json()

templates = search_payload.get("data", {}).get("templates", [])
template = next(
    (item for item in templates if item.get("slug") == TARGET_SLUG),
    None,
)
if not template:
    raise RuntimeError(f"Template not returned: {search_payload}")
if "Cloud" not in template.get("executionMode", []):
    raise RuntimeError("The selected template is not cloud-executable")

print(template["templateId"], template["inputSchema"], template.get("outputSchema"))

From the response, capture and save:

  • recommendedTemplateName
  • The exact template ID and slug
  • executionMode
  • The complete inputSchema
  • The complete sourceTree
  • sourceSummary, when returned
  • outputSchema

Confirm that the selected template is cloud-executable. A local-only template cannot be started with executeTask.

Step 2: Build Parameters from the Input Schema

The live schema returned one required field: youTube_Account_URLs. It is a multi-input field with the value format string[], so send an array even when the run contains one channel.

Use a complete public channel Videos page URL that you are permitted to process:

channel_url = "https://www.youtube.com/@Octoparsewebscraping/videos"

parameters = {
    "youTube_Account_URLs": [channel_url],
}

Recheck this field in the current schema before every production rollout. The field name is case-sensitive, and the verified template expects full channel URLs rather than handles alone.

Step 3: Configure Authentication

Octoparse API requests use x-api-key. Task creation also requires x-external-user-id:

EXTERNAL_USER_ID = os.environ["OCTOPARSE_EXTERNAL_USER_ID"]

task_headers = {
    "content-type": "application/json",
    "x-api-key": API_KEY,
    "x-external-user-id": EXTERNAL_USER_ID,
}

Do not log these headers. In a production application, load the key from a secret manager and rotate it according to your organization’s credential policy.

Step 4: Create the Cloud Task Once

The parameters property must contain a serialized JSON string. This example uses the exact template name and input field observed in the verified discovery response.

import json

task_body = {
    "templateName": "youtube-channel-scraper-free",
    "taskName": "YouTube channel videos - Octoparse",
    "parameters": json.dumps(parameters, separators=(",", ":")),
    "targetMaxRows": 10,
}

if os.environ.get("CONFIRM_OCTOPARSE_EXECUTE") != "yes":
    raise RuntimeError(
        "Set CONFIRM_OCTOPARSE_EXECUTE=yes only after checking the target and account."
    )

execute_response = requests.post(
    f"{BASE_URL}/api/agentTools/executeTask",
    headers=task_headers,
    json=task_body,
    timeout=60,
)
execute_response.raise_for_status()
execute_payload = execute_response.json()

task_data = execute_payload.get("data", {})
if task_data.get("status") not in {"accepted", "ready"}:
    raise RuntimeError(f"Task was not accepted: {execute_payload}")

task_id = task_data["taskId"]
lot_no = task_data.get("lotNo")

Call executeTask only once. If the client times out after the server receives the request, reconcile the saved task before resubmitting. In the test, targetMaxRows was 10 while the final response reported 50 rows, so treat it as a stop target rather than an exact output or billing guarantee.

An accepted response means the task was created and cloud extraction was requested. Save its taskId; the export is not ready yet.

Step 5: Poll Export Status Safely

Poll serially and follow the API’s returned retryGuidance, suggestedNextCall, or workflow instructions. A sensible client should also have hard limits so a stuck task cannot run an infinite loop.

import time

MAX_ATTEMPTS = 30
MAX_ELAPSED_SECONDS = 15 * 60
started_at = time.monotonic()

task_data = execute_payload.get("data", {})
task_id = task_data.get("taskId")
if not task_id:
    raise RuntimeError(f"No taskId in executeTask response: {execute_payload}")

wait_seconds = task_data.get("retryGuidance", {}).get("waitSecondsMin", 60)

for attempt in range(1, MAX_ATTEMPTS + 1):
    if time.monotonic() - started_at >= MAX_ELAPSED_SECONDS:
        raise TimeoutError("Export did not finish within 15 minutes")

    time.sleep(max(1, int(wait_seconds)))

    export_response = requests.get(
        f"{BASE_URL}/api/agentTools/exportData",
        params={
            "taskId": task_id,
            "exportFileType": "JSON",
            "previewRows": 5,
        },
        headers={"x-api-key": API_KEY},
        timeout=60,
    )
    export_response.raise_for_status()
    export_payload = export_response.json()
    export_data = export_payload.get("data", {})
    status = export_data.get("status")

    if status == "exported":
        print("Rows:", export_data.get("dataTotal"))
        print("Export URL:", export_data.get("exportFileUrl"))
        print("Preview:", export_data.get("sampleData"))
        break

    if status == "no_data":
        raise RuntimeError("Task completed but returned no rows")

    if status in {"failed", "invalid"}:
        raise RuntimeError(f"Export stopped with status {status}: {export_payload}")

    if status not in {"collecting", "exporting"}:
        raise RuntimeError(f"Unexpected export status: {export_payload}")

    guidance = export_data.get("retryGuidance", {})
    wait_seconds = guidance.get("waitSecondsMin", wait_seconds)
else:
    raise TimeoutError("Export did not finish within 30 polling attempts")

The loop handles the documented terminal states and limits polling to 30 attempts or 15 minutes, whichever comes first. It does not automatically download the export file. Your application can present the export URL and preview first, then download or analyze the file when that behavior is explicitly required.

What Did the Verified API Run Return?

In the August 14, 2026 first-party test, the URL-based template completed the asynchronous Octoparse API workflow in approximately 260 seconds and reported 50 video records. All five preview rows contained a title, video URL, duration, visible view count, and publication date. This is the populated extraction evidence used for the Python example.

Sanitized first-party Octoparse API export response showing a populated YouTube dataset

What Did the August 30–31 MCP Live Run Return?

The retest followed Octoparse’s current official MCP workflow: discover a template, execute the task, then export the results. OAuth was renewed through the documented Codex login path before the direct MCP tools were called.

StageObserved resultWhat it means
search_templatesID 1813; Cloud and Local; required youTube_Account_URLs string array; ten output fieldsThe current schema matches the Python example
execute_taskAccepted; status runningThe cloud task was created exactly once with only the schema-defined input
Status checkRunning; 199 collected rowsCollection remained active when the latest snapshot was read
export_data199 rows; 40 pages; five populated preview rowsStructured data was available before the run reported completion
Sanitized terminal screenshot of the live Octoparse MCP template discovery, task execution, status, and export stages
Live MCP run started August 30; the latest snapshot was captured August 31, 2026 (Asia/Shanghai), while the task still reported running. Private task, lot, request, OAuth, and signed-export identifiers are redacted.

All five preview rows contained channel context, title, video and cover URLs, duration, visible view count, and date. The result verifies the MCP path for this template, account, target, and test time. It does not make 199 a fixed final total, because the status was still running when the snapshot was exported.

Sanitized MCP Preview

TitleDurationVisible viewsDate
How to Scrape Business Data from Google Maps with Octoparse | Cat Cafe Example4:31579 views2026/03/30
How to Scrape YouTube Comments and Replies Fast2:16900 views2026/01/30
How to Scrape Indeed Job Listings Fast1:28756 views2026/01/30
How to scrape TikTok comments and video details Fast2:112.5K views2025/11/30
Agentic Automation with Human-in-Loop — Octoparse AI Copilot0:53437 views2025/11/30
Sanitized terminal screenshot of five populated rows returned by the live Octoparse MCP export_data call
Five populated preview rows returned by the same live MCP export. URLs and private identifiers are intentionally omitted.

What Is a YouTube Scraper API?

A YouTube scraper API is an interface between your application and a managed web-scraping workflow. Instead of writing code to launch a browser, scroll a channel page, identify video cards, and recover from layout changes, you send a request to a service that performs those steps for you.

The typical workflow has three parts:

  1. Discovery: Find a scraper that supports the page and data you need.
  2. Execution: Supply valid inputs and create a cloud extraction task.
  3. Export: Monitor the asynchronous task and retrieve its structured output.

This is different from a simple synchronous API call. A channel with hundreds of videos may require several minutes of browsing and extraction. For that reason, the first successful response usually means the job was accepted—not that the data is ready.

The distinction matters in production. If your code treats an accepted task as a finished export, it will return no dataset. If it submits a new task every time a network request times out, it can create duplicates. A reliable integration needs to preserve the task ID and treat execution and export as separate states.

YouTube Scraper API vs. YouTube Data API

The official YouTube Data API and a scraper API solve overlapping but different problems. The official API is the natural first choice when its supported resources, fields, and policies fit your application. According to Google’s YouTube Data API overview, it requires a Cloud project and credentials, uses a quota system, and requires OAuth 2.0 for operations involving authorization or private user data.

Official quota reference (checked September 1, 2026): Google documents a separate daily allowance of 100 search.list calls, with each call costing one unit. A channels.list request costs one unit, while the default combined allowance for other endpoints is 10,000 units per day. These defaults can change, so confirm the current quotas in your Google Cloud project before choosing an architecture.

A scraper API works from information publicly displayed on webpages. It may be useful when your project needs a web-visible dataset in a format provided by an existing scraping template. It is not an “unlimited” or policy-free version of the official API.

YouTube API comparison

FactorYouTube Data APIYouTube scraper API
SourceOfficial YouTube API resourcesPublicly accessible webpage content
SetupGoogle Cloud project, API credentials, and OAuth for authorized operationsProvider credentials, a compatible template, and valid template inputs
LimitsQuota units and method-specific costsProvider task, plan, and extraction limits
Response patternOften a direct resource responseCommonly an asynchronous task followed by export polling
MaintenanceStable documented resource modelScraping workflow must adapt to webpage changes
Best fitSupported YouTube features and official integrationsRepeatable collection of a template’s public web dataset

The two methods can also coexist. For example, a team might use the official API for supported product features and use a scraper for an internal research dataset that reflects what visitors can see on public channel pages.

What Data Can You Extract from YouTube Channels?

The verified YouTube Channel Scraper (Free) workflow is designed for public channel and video metadata. Its observed schema and sample included:

https://www.octoparse.com/template/youtube-channel-scraper-free

  • Video title
  • Author or channel account
  • Video URL
  • Publish time
  • View count
  • Video length

The page also indicates that users can submit multiple channel handles. It does not describe the template as a comments or transcript extractor, so those are outside this tutorial’s scope.

Treat the template’s live API schema as the source of truth. Marketing copy and screenshots help you understand the product, but an integration must use the field keys and allowed options returned by searchTemplates. Never infer an API key from a label such as “Usernames to Scrape,” because the machine-facing key can be different.

Why Use Octoparse for a YouTube Scraping Workflow?

Building a YouTube crawler yourself gives you control, but it also makes your team responsible for browser orchestration, scrolling behavior, selector changes, retries, infrastructure, and export logic. Octoparse packages the extraction workflow as a reusable template and exposes programmatic task management and export through the Octoparse OpenAPI reference.

That division of labor is useful when you want to:

  • Send YouTube data into an existing Python pipeline
  • Run the same collection method for many public channels
  • Export structured data without operating a browser cluster
  • Keep application code focused on validation, orchestration, and analysis

The benefit is not “zero engineering.” You still need to select the correct template, validate parameters, secure credentials, manage task state, and handle failures. The difference is that you are integrating a managed scraping workflow instead of rebuilding the crawler layer.

Python Implementation Checklist Before Production

The examples use values observed in one successful API run. Template metadata can change, so revalidate the integration instead of treating this article as a permanent schema contract.

Verified Values and Production Checks

ItemObserved valueProduction check
Templateyoutube-channel-scraper-free, ID 1813Match the current slug and Cloud execution mode
Input fieldyouTube_Account_URLsConfirm string[] in inputSchema
Input formatFull channel /videos URLValidate each permitted URL before task creation
OutputTen channel and video fieldsMap the current schema and inspect a small sample
Polling60-second guidance in the testFollow the value returned for the current task
Row targetRequested 10; response reported 50Do not assume an exact hard cap

Before deployment, combine and compile the Python code, mock every terminal export state, and run one small authorized end-to-end sample. Persist the task ID before retrying any ambiguous task-creation request.

Common Errors and How to Handle Them

Octoparse API export polling statuses

YouTube scraper API troubleshooting table

SymptomLikely causeRecommended action
401 or 403Missing, invalid, or unauthorized API keyVerify the key and template access without exposing credentials in logs
invalid with missing parametersRequest does not match inputSchemaAdd the named field using its exact schema key
awaiting_source_selectionA source-backed option is unresolvedInspect sourceTree and send the option key
accepted but no fileExtraction is asynchronousWait according to retryGuidance, then call exportData
Repeated duplicate tasksRetrying executeTask after an ambiguous timeoutPersist identifiers and reconcile before resubmitting
collecting or exportingTask or export is still in progressContinue bounded serial polling
no_dataTarget, parameters, or public page returned no rowsCheck the channel input and template requirements
failedExtraction or export failedInspect error details and retry only when the response says recovery is possible

Avoid generic retry decorators around task creation. Retries are useful for idempotent reads, but cloud-task creation can have side effects even when the client never receives the response.

How Fast Is a YouTube Channel Scraper?

There is no single reliable runtime for every channel. Channel size, page behavior, cloud load, template configuration, and retry conditions can all affect completion time. A small public channel may finish quickly, while a large catalog can require a longer extraction and export cycle.

Use a representative sample of your own target channels before setting production timeouts or service-level expectations. In Octoparse’s 2026 HubSpot YouTube competitor-analysis test, a channel task returned 364 videos in 4 minutes 24 seconds with no failed URLs. A follow-on task across 10 videos returned 124 comments in 1 minute 21 seconds with no duplicates. These are observed results for those targets and settings, not general throughput guarantees.

Practical YouTube Scraper API Use Cases

Competitor channel monitoring

Collect public video titles, dates, durations, and visible view counts on a schedule. A downstream job can calculate posting frequency, identify recurring formats, and flag unusually strong videos for human review.

Creator research

Build a consistent dataset of public channel videos for an initial creator shortlist. Scraped metadata can support discovery and comparison, but human review should remain part of any partnership decision.

Content research

Group titles by topic, compare publishing cadence, or analyze how video length varies across a channel. The API handles repeatable collection; your analysis layer handles normalization and interpretation.

For adjacent collection needs, use the YouTube channel crawler guide for no-code channel exports and the YouTube comment scraper guide for comment-specific workflows. This tutorial remains focused on the Python task lifecycle.

Dataset enrichment

Join public YouTube metadata to an existing list of companies, products, or creators. Preserve collection timestamps because values such as visible view counts change over time.

Public visibility is not blanket permission to collect or reuse information for every purpose. YouTube’s current Terms of Service restrict automated access except in specified circumstances, including prior written permission or where applicable law permits it. They also restrict collecting information that may identify a person unless an applicable permission or legal basis exists.

Before running a scraper:

  • Review YouTube’s current Terms of Service and applicable policies.
  • Confirm that your collection and intended use are lawful in relevant jurisdictions.
  • Limit the workflow to data you are authorized to collect.
  • Avoid private, restricted, or sensitive personal information.
  • Do not use the workflow to download videos or bypass access controls.
  • Apply reasonable volume, retention, and security controls.
  • Seek legal advice when the use case or jurisdiction creates uncertainty.

This article explains a technical integration and is not legal advice.

FAQs about YouTube Scraper API

  1. Is there a YouTube scraper API?

Yes. Third-party scraping providers can expose public YouTube data collection as an API workflow. The Octoparse API lets an application discover a supported template, start a cloud task, and export the resulting dataset.

  1. Can I scrape YouTube with Python?

Python can orchestrate a managed YouTube scraper through HTTP requests. You need provider credentials, an exact cloud-executable template, parameters built from its current schema, and code that handles asynchronous task states.

  1. Is a YouTube scraper API an alternative to the YouTube Data API?

It can be an alternative for some public web-data research tasks, but it is not a drop-in replacement. The official API provides documented YouTube resources and authorization flows; a scraper API collects the public webpage dataset supported by its template. Choose based on your fields, policies, operational needs, and permitted use.

  1. What can the Octoparse YouTube Channel Scraper collect?

The active template page shows public video metadata including titles, URLs, publish times, view counts, lengths, and channel or author information. Confirm the exact current output through outputSchema and an exported sample before building downstream mappings.

  1. Do I need to build an Octoparse desktop task first?

Not when a suitable cloud-executable template already exists. The Octoparse API discovery endpoint can return supported templates and their input schemas. A local-only template, however, cannot be started through executeTask.

  1. Why does the API return accepted without data?

Scraping and export take place asynchronously. accepted confirms task creation and the cloud-start request. Save the task ID, follow the returned retry guidance, and call exportData until the status becomes exported or another terminal state is returned.

  1. Is it legal to scrape public YouTube data?

Legality and contractual permission depend on the data, method, jurisdiction, and intended use. Public accessibility alone does not settle the question. Review YouTube’s current terms, protect personal information, and obtain professional legal advice when needed.

Build the Integration Around the Task Lifecycle

A dependable YouTube scraper API integration is less about sending one POST request and more about controlling the full lifecycle. Discover the exact template, derive parameters from its live schema, create the task once, preserve the task ID, and poll the export endpoint within clear limits.

That pattern gives your application a clean boundary: Octoparse operates the browser-based extraction workflow, while your Python service validates inputs, tracks task state, and routes the exported dataset to its destination.

The template schema and one populated API execution/export sequence have been verified for this example. Before production use, repeat discovery, compile the combined script, test every terminal status, and run one small permitted sample because template behavior can change.

Get Web Data in Clicks
Easily scrape data from any website without coding.
Free Download
image
Get web automation tips right into your inbox
Subscribe to get Octoparse monthly newsletters about web scraping solutions, product updates, etc.

Get started with Octoparse today

Free Download

Related Articles