How to Scrape YouTube Video Results - 2024 Guide

YouTube, is the world's largest online video-sharing platform. YouTube is also one of the most visited websites on the internet. And often referred to as the second-largest search engine, after Google itself.

Youtube hosts a vast array of content, videos are indexed by search engines so that content creators can leverage search engine optimization(SEO) techniques to rank their videos. Furthermore, scraping YouTube videos can provide critical insights, upcoming trends, competitive analysis, and content ideas.

YouTube created a healthy platform for content creators. They applied cutting-edge technology to deter fake users from fake video view counts. Scraping YouTube videos is never an easy task. But with SerpApi, you don't need to worry about this. You just need to pick up our YouTube solution and grow your business.

Setting up a SerpApi account

SerpApi offers a free plan for newly created accounts. Head to the sign-up page to register an account and complete your first search with our interactive playground. When you want to do more searches with us, please visit the pricing page.

Once you are familiar with all results, you can utilize SERP APIs using your API Key.

Scrape your first YouTube Video Results with SerpApi

Head to the YouTube Video results from the documentation on SerpApi for details.

In this tutorial, we will scrape and extract all coffee videos from YouTube search with Python. The data contains: "position", "title", "link", "views", "channel_name", "description", "thumbnail", "published_date", and more. You can also scrape more information with SerpApi.

First, you need to install the SerpApi client library.

pip install google-search-results

Set up the SerpApi credentials and search.

from serpapi import GoogleSearch
import os, json

params = {
    'api_key': 'YOUR_API_KEY',         # your serpapi api
    'engine': 'youtube',               # SerpApi search engine	
    'search_query': 'Coffee'           # query
}

To retrieve the YouTube video results for a given search query, you can use the following code:

results = GoogleSearch(params).get_dict()['video_results']

You can store YouTube video results JSON data in databases or export them to a CSV file.

import csv

header = ['position', 'title', 'link', 'views', 'channel_name', 'description', 'thumbnail']

with open('youtube_video_results.csv', 'w', encoding='UTF8', newline='') as f:
    writer = csv.writer(f)

    writer.writerow(header)

    for item in results:
        print(item)
        writer.writerow([item.get('position_on_page'), item.get('title'), item.get('link'), item.get('views'), item.get('channel', {}).get('name'), item.get('description'), item.get('thumbnail', {}).get('static'), item.get('published_date')])

Interesting scraping YouTube Shorts videos, I already have a blog post for this. Check out at https://serpapi.com/blog/how-to-scrape-youtube-shorts-videos-results/

If you have any questions, please feel free to contact me.