How to get YouTube video views and likes in Python
Checked on 2026-11-18.
The page of a YouTube video shows rounded numbers such as "1.7M views". From code you can get the exact ones. Here are three ways, and for long lists of videos the official API is the one to use.
1. The official YouTube Data API
videos.list costs 1 quota unit per call, and one call takes several video IDs separated by commas. With 10,000 units a day by default, this is the cheapest way to read numbers for many videos. You need a Google Cloud project and an API key.
import os
import requests
ids = ["jNQXAC9IVRw", "dQw4w9WgXcQ"]
page = requests.get(
"https://www.googleapis.com/youtube/v3/videos",
params={
"part": "snippet,statistics,contentDetails",
"id": ",".join(ids),
"key": os.environ["YOUTUBE_API_KEY"],
},
timeout=30,
).json()
for item in page["items"]:
stats = item["statistics"]
print(
item["snippet"]["title"],
int(stats["viewCount"]),
stats.get("likeCount"), # missing when the owner hides likes
stats.get("commentCount"), # missing when comments are turned off
item["contentDetails"]["duration"], # ISO 8601, such as PT15M33S
)Three details save time. The counts arrive as text, so convert them with int(). The length is an ISO 8601 duration: PT15M33S means 15 minutes and 33 seconds. And the dislike count is no longer public: Google's reference says it is only returned to the video's owner.
2. yt-dlp, for a few videos on your own computer
yt-dlp --print "%(view_count)s %(like_count)s %(duration)s %(title)s" "https://www.youtube.com/watch?v=jNQXAC9IVRw"It prints the view count, the likes, the length in seconds and the title, and downloads nothing. No key and no quota. From cloud servers YouTube often refuses these requests.
3. An HTTP API, without a Google project
With Pipefetch one request returns the exact counts together with the description, tags, chapters and the channel's subscriber count.
import os
import requests
res = requests.get(
"https://pipefetch.com/v1/youtube/video",
params={"video": "https://www.youtube.com/watch?v=jNQXAC9IVRw"},
headers={"x-api-key": os.environ["PIPEFETCH_API_KEY"]},
timeout=90,
)
if res.status_code == 404:
print("Video not found, private or restricted") # not charged
else:
res.raise_for_status()
video = res.json()["data"]
print(video["title"], video["viewCount"], video["likeCount"], video["commentCount"], video["durationSeconds"])The numbers are already numbers and the length is in seconds. It is one video per request and 1 credit each, so for thousands of videos the official API is cheaper. The reference lists every field.
Which one should you use?
| Situation | Use |
|---|---|
| Numbers for hundreds or thousands of videos | The official API: many IDs per call, 1 unit each call |
| A quick look at a few videos on your own computer | yt-dlp |
| A server, no Google Cloud project, or you also want tags, chapters and the channel in the same answer | An HTTP API such as Pipefetch |
For the latest videos of one channel with their view counts in a single request, see the guide on listing a channel's videos.
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 Channel Videos API
The latest uploads of a channel, newest first, with views, duration and publish time.
GET /v1/youtube/channel/videos
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 search YouTube from Python
- How to list a YouTube channel's videos in Python
- How to summarize a YouTube video with an LLM in Python
- How to get YouTube transcripts and video stats in Google Sheets
- How to get YouTube transcripts in n8n, Make and Zapier
- LangChain YoutubeLoader fails on a server: why, and a loader that works
- How to get a YouTube channel's subscriber count in Python
- How to give Claude or Cursor access to YouTube (MCP server)