Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/vsmutok/ytscrape/llms.txt

Use this file to discover all available pages before exploring further.

The YouTube.search() method is the primary way to query YouTube. It accepts a text query and an optional filter and returns a SearchResults object — a lazy, paginated iterable that streams results across as many pages as you need. With no filter applied, YouTube returns a mixed feed of videos, channels, and playlists. Iterate directly over the results to consume them one at a time; pages are loaded on demand.
from ytscrape import YouTube

with YouTube() as yt:
    for item in yt.search("python tutorial"):
        print(item.title, item.url)
Pass max_results to stop after a fixed number of items:
with YouTube() as yt:
    for item in yt.search("python tutorial", max_results=10):
        print(item.title)

Filtering by result type

Import SearchFilter and pass it as the filter keyword argument to restrict results to a single content type.
FilterString valueDescription
SearchFilter.ALL"all"Mixed feed — videos, channels, and playlists (default)
SearchFilter.VIDEOS"videos"Videos only
SearchFilter.CHANNELS"channels"Channels only
SearchFilter.PLAYLISTS"playlists"Playlists only
SearchFilter.SHORTS"shorts"YouTube Shorts only
SearchFilter.MOVIES"movies"Movies only

Videos

from ytscrape import YouTube, SearchFilter

with YouTube() as yt:
    results = yt.search(
        "python tutorial",
        filter=SearchFilter.VIDEOS,
        max_results=10,
    )
    for video in results:
        print(f"{video.title}  ({video.duration})")
        print(f"  by {video.channel}{video.views}")
        print(f"  {video.url}")

Channels

from ytscrape import YouTube, SearchFilter

with YouTube() as yt:
    for channel in yt.search("python", filter=SearchFilter.CHANNELS, max_results=5):
        print(f"{channel.title}{channel.subscribers}")
        print(f"  {channel.url}")

Playlists

from ytscrape import YouTube, SearchFilter

with YouTube() as yt:
    for playlist in yt.search("python", filter=SearchFilter.PLAYLISTS, max_results=5):
        print(f"{playlist.title} ({playlist.video_count} videos)")
        print(f"  {playlist.url}")

Using string values instead of the enum

Every SearchFilter member has an equivalent lowercase string value. You can pass it directly without importing the enum:
with YouTube() as yt:
    for video in yt.search("lofi", filter="videos", max_results=5):
        print(video.title)
Valid string values are "all", "videos", "channels", "playlists", "shorts", and "movies". An unrecognised value raises ValueError immediately.

The max_results parameter

max_results caps the total number of items yielded during iteration. Once that count is reached the iterator stops cleanly, even if YouTube has more pages.
with YouTube() as yt:
    # Fetch at most 25 results across however many pages that requires.
    for video in yt.search("machine learning", filter="videos", max_results=25):
        print(video.title)
Omit max_results entirely to consume every result YouTube returns for the query.

Return type: SearchResults

yt.search() returns a SearchResults object. It is a lazy iterable — the first page of results is fetched when search() is called, and additional pages are fetched automatically as you consume items past the end of each page. You can also drive pagination manually — see the Pagination guide for details.

Result field reference

Each yielded item is a frozen, fully typed dataclass. The concrete type depends on the active filter.

Video — returned by ALL and VIDEOS

FieldTypeDescription
video_idstrUnique 11-character YouTube video id
titlestr | NoneVideo title
channelstr | NoneDisplay name of the uploading channel
channel_idstr | NoneUC… channel id
durationstr | NoneFormatted duration string (e.g. "10:32")
viewsstr | NoneView count as rendered by YouTube (e.g. "1.2M views")
publishedstr | NoneRelative publish time (e.g. "3 days ago")
thumbnailstr | NoneURL of the largest available thumbnail
urlstrCanonical https://www.youtube.com/watch?v=… URL (computed property)

Channel — returned by ALL and CHANNELS

FieldTypeDescription
channel_idstrUC… channel id
titlestr | NoneChannel display name
handlestr | None@handle when available
subscribersstr | NoneSubscriber count as rendered by YouTube (e.g. "1.23M subscribers")
video_countstr | NonePublic video count
thumbnailstr | NoneURL of the channel avatar
urlstrCanonical https://www.youtube.com/channel/UC… URL (computed property)

Playlist — returned by ALL and PLAYLISTS

FieldTypeDescription
playlist_idstrUnique playlist id
titlestr | NonePlaylist title
channelstr | NoneName of the channel that owns the playlist
video_countstr | NoneNumber of videos in the playlist
thumbnailstr | NoneURL of the playlist thumbnail
urlstrCanonical https://www.youtube.com/playlist?list=… URL (computed property)
Create one YouTube instance and reuse it across multiple searches. The instance holds a warm HTTP session and a cached InnerTube context, so subsequent calls skip the initial context-extraction request and run noticeably faster.

Build docs developers (and LLMs) love