> ## Content Index
> Fetch the complete content index at: https://serpapi.com/blog/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to scrape YouTube data with a simple API (using Python)
- URL: https://serpapi.com/blog/how-to-scrape-youtube-data-with-simple-api/
- Published: 2024-02-19T06:23:41.000Z
- Updated: 2025-09-09T01:19:33.000Z
- Description: Learn how to scrape YouTube data with a simple API using Python. Bonus: we will also scrape the video detail.
- Author: Hilman Ramadhan
- Tags: YouTube, Python

If you're interested in extracting information from YouTube easily, you're in the right place! Whether you're working on a personal project, academic research, or professional analysis, understanding how to gather data from YouTube can be incredibly useful. We'll be using [YouTube Search API](https://serpapi.com/youtube-search-api).

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/02/scrape-youtube-data-with-python-1.webp)

Scrape YouTube data with Python illustration.

## Video Tutorial

If you prefer to watch a video, feel free to take a look at this one:

### Tools Introduction

We'll use the new official Python library by SerpApi: [serpapi-python](https://github.com/serpapi/serpapi-python) .  
That's the only tool that we need!

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2023/10/tools-to-scrape-google-search-in-python.webp)

Python library from SerpApi, the tool we need to scrape Google SERP

> As a side note: You can use this library to scrape search results from other search engines, not just YouTube.

Usually, you'll write your DIY solution using something like BeautifulSoup, Selenium, Puppeteer, Requests, etc., to scrape YouTube. You can relax since we perform all these heavy tasks for you. No need to worry about all the problems you might've encountered while implementing your web scraping solution.

## Step-by-step scraping YouTube data with Python

Without further ado, let's start and collect data from the YouTube site.

### Step 1: Setup and preparation

- Sign up for free at [SerpApi](https://serpapi.com/). You can get 100 free searches per month.
- Get your SerpApi Api Key from [this page](https://serpapi.com/manage-api-key).
- Create a new `.env` file, and assign a new env variable with value from API\_KEY above.  
`SERPAPI_KEY=$YOUR_SERPAPI_KEY_HERE`
- Install python-dotenv to read the `.env` file with  
`pip install python-dotenv`
- Install SerpApi's Python library with  
`pip install serpapi`
- Create new `main.py` file for the main program.

Your folder structure will look like this:

```
|_ .env
|_ main.py

```

### Step 2: Write the code for scraping YouTube search result

To use the YouTube Search API, we only need the search query as a parameter. Here is how the basic search works:

```python
import os
import serpapi
from dotenv import load_dotenv

# Load env file and API_Key
load_dotenv()
api_key = os.getenv('SERPAPI_KEY')

# Perform a basic search
client = serpapi.Client(api_key=api_key)
search =  client.search(
    engine="youtube",
    search_query="fit and fun" # You can change this query to anything
  )

print(search)
```

Here is the JSON response. As we can see, it matches what YouTube is actually showing.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/02/CleanShot-2024-02-19-at-07.36.16.png)

Youtube scrape result

Optional: You can also add a `gl` or `hl` parameter to adjust the result with the geolocation or language that you want.

## Paginate YouTube result

By default, this API only returns the first page of the results. Just like searching on the YouTube site directly, we need to do a pagination in order to grab all the results.

You can add a token for the next page on the `sp` parameter. We provide this `next_page_token` at each response on the `serpapi_pagination > next_page_token response.`

Here is a simple example for grabbing the next page:

```python
client = serpapi.Client(api_key=api_key)
search =  client.search(
    engine="youtube",
    search_query="fit and fun",
  )

if "serpapi_pagination" in search:
  search = client.search(
    engine="youtube",
    search_query="fit and fun",
    sp=search["serpapi_pagination"]["next_page_token"]
  )
# second page result
```

Now let's automate this process with a while loop:

```python
client = serpapi.Client(api_key=api_key)
search =  client.search(
    engine="youtube",
    search_query="fit and fun",
  )
# first page results, print number of video results
print(len(search["video_results"]))

MAX_PAGES = 10
while "serpapi_pagination" in search:
  search = client.search(
    engine="youtube",
    search_query="fit and fun",
    sp=search["serpapi_pagination"]["next_page_token"]
  )
  print(len(search["video_results"]))

  # It's recommended to limit the number of pages to avoid infinite loop
  MAX_PAGES -= 1
  if MAX_PAGES <= 0:
    break

print("done")
```

## What data is available from this API?

You're able to get the following data:

- search video results
- Youtube ads\_results
- Top news / latest
- Channel result
- Movie result
- Playlist result
- Shorts result

## BONUS: Scraping video detail on Youtube

You can also get the video detail data from [YouTube Video API](https://serpapi.com/youtube-video-api). You'll need the video ID as a parameter. Use the video ID as `v parameter` value.

![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/2024/02/CleanShot-2024-02-19-at-07.59.19.png)

Youtube Video API illustration

Here is the code sample:

```python
client = serpapi.Client(api_key=api_key)
search =  client.search(
    engine="youtube_video",
    v="DK-nzoXukbg"
)

print(search)
```

With this, you can collect information about this video including:

- title
- thumbnail
- views
- likes
- description
- chapters
- related\_videos

Here is the complete tutorial for scraping YouTube videos:

[Scrape YouTube videos in PythonLearn how to scrape YouTube videos using a simple API by SerpApi in Python.![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/icon/serpapi-favicon-107.png)SerpApiHilman Ramadhan![](https://storage.ghost.io/c/a5/00/a5004977-0dd2-4bcd-9292-dd0e05d4c59e/content/images/thumbnail/scrape-youtube-video.webp)](https://serpapi.com/blog/scrape-youtube-videos-in-python/)

That's it! I hope you enjoyed reading this guide.