Python · YouTube
How to scrape YouTube with Python
Scraping YouTube with a browser means scrolling infinite lists, waiting for scripts and parsing markup that changes often. The official YouTube Data API needs a Google Cloud project and rations requests with a daily quota. This guide takes a third route: you scrape YouTube with Python by calling the everydata.io API, which reads the public YouTube pages and returns them as JSON.
You build three small scripts: a YouTube scraper for search results that pages through the results and saves them to CSV, one that lists the latest videos of a channel, and one that fetches the full details of a video – views, likes, comment count, publish date, duration, category and tags.
Updated · by everydata.io
What you will have at the end
- A Python script that scrapes YouTube search results for any query into videos.csv.
- The latest videos of any channel by channel ID, handle URL or channel URL.
- Full video details by video ID, with a reusable helper that handles rate limits and retries.
What you need
- Python 3.10 or newer and the
requestspackage (pip install requests). - A free everydata.io API key – create one here.
- A search query, a channel or a video you want to scrape.
01Why scrape YouTube through an API?
YouTube builds its pages with JavaScript and loads more results as you scroll, so a plain HTTP request gets little usable data and a headless browser has to scroll, wait and parse. The everydata.io YouTube API does that work on the server: each call returns one page of results, with a cursor for the next one.
Compared with the official YouTube Data API there is no OAuth screen, no Google Cloud project and no daily unit quota – only your everydata.io key. Every call is one request on your plan, and answers with a 5xx status are not counted.
02Write a small request helper
Put your key into the environment (export EVERYDATA_API_KEY="ds_your_key") and save this helper as everydata.py. It waits when you send too many requests at once (status 429, the answer says how many seconds) and retries a page that failed to load (5xx) once.
import os
import re
import time
import requests
API = "https://api.everydata.io"
KEY = os.environ["EVERYDATA_API_KEY"]
def api_get(path: str, params: dict) -> dict:
"""GET an everydata.io endpoint. Waits when rate limited (429) and retries a failed page load (5xx) once."""
retried = False
for _ in range(4):
resp = requests.get(f"{API}{path}", params=params, headers={"x-api-key": KEY}, timeout=60)
if resp.status_code == 429:
wait = re.search(r"in (\d+) seconds", resp.text)
time.sleep(int(wait.group(1)) + 1 if wait else 60)
continue
if resp.status_code >= 500 and not retried:
retried = True # 5xx answers are not counted against your quota
time.sleep(10)
continue
resp.raise_for_status()
return resp.json()
raise RuntimeError("still rate limited after several waits – try again later")03Scrape YouTube search results into a CSV file
The search endpoint takes a query plus optional languageCode and countryCode (default en and US) and returns the result list in items and a cursor for the next page. Results mix videos, channels and playlists, so the script keeps only type video.
"""Scrape YouTube search results for a query into videos.csv – several pages via the cursor."""
import csv
from everydata import api_get # the helper from step 2, saved as everydata.py
QUERY = "lofi hip hop"
MAX_PAGES = 3 # every page is one request
rows, cursor = [], None
for page in range(1, MAX_PAGES + 1):
params = {"query": QUERY, "languageCode": "en", "countryCode": "US"}
if cursor:
params["cursor"] = cursor
data = api_get("/ytb/youtube-search-videos", params)
for item in data["items"]:
if item["type"] != "video": # results also contain channels and playlists
continue
rows.append({
"page": page,
"video_id": item["videoId"],
"title": item["title"],
"channel": item["channelName"],
"views": item["viewCount"],
"published": item["publishedTimeText"],
"duration_s": item["duration"],
"live": item["isLive"],
})
cursor = data.get("cursor")
if not cursor:
break
with open("videos.csv", "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=list(rows[0]))
writer.writeheader()
writer.writerows(rows)
print(len(rows), "videos saved to videos.csv")A shortened version of the documented example for the query lofi hip hop:
{
"responseStatus": "PAGE_FOUND",
"responseMessage": "Page successfully found!",
"query": "lofi hip hop",
"estimatedResults": 2407972,
"items": [
{
"type": "video",
"videoId": "n61ULEU7CO0",
"title": "Best of lofi hip hop 2021 ✨ [beats to relax/study to]",
"channelId": "UCSJ4gkVC6NrvII8umztf0Ow",
"channelName": "Lofi Girl",
"viewCount": 57439529,
"viewCountText": "57,439,529 views",
"publishedTimeText": "4 years ago",
"duration": 22258,
"durationText": "6:10:58",
"isLive": false
}
]
}viewCount is a number you can sort and sum; viewCountText is the label YouTube shows. For live streams the label reads "watching" and duration is null. publishedTimeText is relative ("4 years ago") on search results – the video details call returns the exact publishedAt.
04Scrape the videos of a YouTube channel
For one channel, pass its channel ID, its handle URL (https://www.youtube.com/@WWE) or its channel URL as id. filter picks the tab: videos_latest (default), streams_latest, shorts_latest or live_now. Page further with cursor, as in the search script.
from everydata import api_get
data = api_get("/ytb/youtube-channel-videos", {"id": "https://www.youtube.com/@WWE", "filter": "videos_latest"})
for item in data["items"]:
print(item["videoId"], item["viewCount"], item["publishedTimeText"], item["title"])
next_page = data["cursor"] # pass as cursor=... for the next page{
"responseStatus": "PAGE_FOUND",
"responseMessage": "Page successfully found!",
"channelId": "UCJ5v_MCY6GNUBTO8-D3XoAg",
"filter": "videos_latest",
"items": [
{
"type": "video",
"videoId": "b7kl_pcHa0A",
"title": "Blake Monroe vows to get payback against Giulia: SmackDown exclusive, Sept. 18, 2026",
"viewCount": 6800,
"viewCountText": "6.8K views",
"publishedTimeText": "47 minutes ago",
"duration": 110,
"durationText": "1:50"
}
]
}05Scrape YouTube video details
Search and channel lists give you video IDs. For the full record of one video – exact view, like and comment counts, publish date, category, tags and more – call the video details endpoint with the ID or the video URL.
from everydata import api_get
v = api_get("/ytb/youtube-video-details", {"id": "kJQP7kiw5Fk"})
print(v["title"], "by", v["channelName"])
print(v["viewCount"], "views,", v["likeCount"], "likes,", v["commentCount"], "comments")
print("published", v["publishedAt"], "| duration", v["durationText"], "| category", v["category"])
print("tags:", ", ".join(v["tags"])){
"responseStatus": "PAGE_FOUND",
"responseMessage": "Page successfully found!",
"videoId": "kJQP7kiw5Fk",
"title": "Luis Fonsi - Despacito ft. Daddy Yankee",
"channelName": "LuisFonsiVEVO",
"viewCount": 9125159593,
"likeCount": 56673581,
"commentCount": 4300000,
"publishedAt": "2017-01-12T21:00:02-08:00",
"duration": 282,
"durationText": "4:42",
"category": "Music",
"tags": [
"Calypso",
"Despacito"
]
}To track these numbers over time, the YouTube video stats guide saves them on a schedule. For the comment section of a video, see the YouTube comments API.
06Costs and limits of scraping YouTube
- Every search page, channel page and video lookup is one request. Three search pages plus details for 20 videos are 23 requests; the free plan includes 100 requests a month.
- Results follow
languageCodeandcountryCode, like YouTube does for a visitor from that country. - Only public data: private, deleted or region-blocked videos answer with HTTP 404 instead of a half-filled object.
Endpoints used in this guide
Search Videos · YouTube API
Channel Videos · YouTube API
Video Details · YouTube API
What else you can do with this data
YouTube metadata as JSON – title, views, likes, comments, duration, tags, thumbnails, captions – by video ID or URL. No OAuth, no Data API quota.
Public YouTube channel stats via REST: subscriber and video counts, verification, latest videos, streams, Shorts and playlists by channel ID or @handle.
Scrape YouTube comments by video ID or URL: text, author, likes, reply count, pinned and hearted flags, with cursor paging into replies. No OAuth.
Try it with your own data
The free plan includes 100 requests a month, usable on every platform – enough to follow this guide end to end.