How to search YouTube from Python

Checked on 2026-11-18.

Searching YouTube from code sounds like a job for the official API, and for a few searches a day it is. The catch is its limit: 100 searches a day by default. Here are the three routes that work, with what each one returns.

1. The official YouTube Data API

Python
import os
import requests

page = requests.get(
    "https://www.googleapis.com/youtube/v3/search",
    params={
        "part": "snippet",
        "q": "web scraping tutorial",
        "type": "video",
        "maxResults": 50,        # 0 to 50, the default is 5
        "order": "relevance",    # date, rating, title, viewCount
        "key": os.environ["YOUTUBE_API_KEY"],
    },
    timeout=30,
).json()
for item in page["items"]:
    print(item["id"]["videoId"], item["snippet"]["title"])

Two things to plan for. First, the quota: Google's documentation gives search.list its own bucket with a default of 100 calls a day, separate from the 10,000 units for everything else. Second, a search result is only a snippet: title, description, channel and publish time. View counts and durations need a second call to videos.list with the IDs you got.

2. yt-dlp

yt-dlp runs a search when the address starts with ytsearch, a number and a colon.

Terminal
yt-dlp "ytsearch10:web scraping tutorial" --flat-playlist --dump-json

It prints one JSON object per result. --flat-playlist keeps it fast by not opening every video, at the price that some fields can be missing. No key and no quota, but from cloud servers YouTube often refuses these requests.

3. An HTTP API, without the daily limit

With Pipefetch a search is one HTTPS request, and each result already has its view count, duration and channel.

Python
import os
import requests

res = requests.get(
    "https://pipefetch.com/v1/youtube/search",
    params={"q": "web scraping tutorial", "limit": 20, "sort": "relevance", "uploaded": "month"},
    headers={"x-api-key": os.environ["PIPEFETCH_API_KEY"]},
    timeout=90,
)
if res.status_code == 404:
    print("No results")  # not charged
else:
    res.raise_for_status()
    for video in res.json()["data"]["results"]:
        print(video["viewCount"], video["durationSeconds"], video["channel"]["name"], video["title"])

sort is relevance, popularity, newest or mostViewed; uploaded narrows to the last hour, day, week, month or year; type switches to Shorts, channels or playlists; country and lang set where the search is run from. A search costs 1 credit whatever the number of results, up to 40 per request, and there are no further pages. The reference has every parameter.

Which one should you use?

SituationUse
Under 100 searches a day, and you already have a Google Cloud projectThe official API
Occasional searches on your own computeryt-dlp
More than 100 searches a day, or view counts in the same request, from a serverAn HTTP API such as Pipefetch

If you already hit the official limit, the guide on the quota exceeded error explains what resets when and how to ask Google for more.

Try it with 100 free credits. No card, no Google Cloud project, no OAuth. Get an API key or read the docs.