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
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.
yt-dlp "ytsearch10:web scraping tutorial" --flat-playlist --dump-jsonIt 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.
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?
| Situation | Use |
|---|---|
| Under 100 searches a day, and you already have a Google Cloud project | The official API |
| Occasional searches on your own computer | yt-dlp |
| More than 100 searches a day, or view counts in the same request, from a server | An 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.
APIs used in this guide
-
1 credit
YouTube Video API
Exact views, likes and comment count, description, tags, chapters and channel for one video.
GET /v1/youtube/video -
1 credit
YouTube Search API
YouTube search results for a query, with the same sorting and filters the site offers.
GET /v1/youtube/search
More guides
- How to get a YouTube transcript in Python
- How to get a YouTube transcript in Node.js
- YouTube API quota exceeded: what the error means and how to fix it
- How to find a YouTube channel ID from a handle or URL
- How to download YouTube subtitles as SRT or VTT
- youtube-transcript-api RequestBlocked or IpBlocked: why it happens and how to fix it
- How to get YouTube comments in Python
- How to list a YouTube channel's videos in Python
- How to summarize a YouTube video with an LLM in Python