In this article, we will dissect a Python script that is designed to generate SEO-optimized content based on a given search query. The script retrieves the top five YouTube videos related to the query and extracts their transcripts. It then feeds these transcripts into OpenAI’s GPT-3 language model to produce a summary of each video. The generated content is compiled in HTML format, which can be saved as an HTML file and viewed in a web browser. We will also explore how to customize this script to fit your needs.

Breaking Down The Code

Importing Required Libraries

The script imports necessary libraries to interface with the YouTube API, OpenAI GPT-3, and perform HTML parsing.

import openai
import webbrowser
from googleapiclient.discovery import build
from youtube_transcript_api import YouTubeTranscriptApi

API Keys and Client Setup

The script initializes API keys for YouTube and OpenAI, and sets up clients for each service.

API_KEY_YOUTUBE = "AIzaSyDnReiZurwoC_K9W2c3sdfdsfsdfmbrDzVv31t5DHi0"
API_KEY_OPENAI = "skmtzVNBuZfdgdgdfgfdgmqgaOJGub56oT3BlbkFJZDBWUDM2rS7zVjLUXuYI"

youtube = build('youtube', 'v3', developerKey=API_KEY_YOUTUBE)
openai.api_key = API_KEY_OPENAI

Searching for YouTube Videos

The buscar_videos function utilizes the YouTube API to search for the top five most viewed videos related to a given search query.

def buscar_videos(query):
    resultados =

    return resultados['items']

Generating Video Content

The generar_contenido_videos function extracts transcripts from the YouTube videos and generates summaries using the GPT-3 language model. The summaries are then formatted in HTML and appended to a list.

def generar_contenido_videos(videos):
    contenido_videos = []

    for video in videos:
        video_id = video['id']['videoId']
        titulo = video['snippet']['title']
        vista_previa = video['snippet']['thumbnails']['medium']['url']

            transcripciones = YouTubeTranscriptApi.get_transcript(video_id)
            prompt_resumen = (f"In the video related to the search query ... ")
            response = openai.Completion.create(...)      
            resumen = response.choices[0].text.strip()

            contenido_video = (f"<h2>{titulo}</h2> ..."

            print("No se encontraron transcripciones para el video:", titulo)

    return contenido_videos

Compiling and Saving Content in HTML Format

The generar_contenido_html function takes the content from the contenido_videos list and converts it into an HTML document, which is then saved to an HTML file in the local directory. A built-in webbrowser library is used to open the saved HTML file in the default web browser.

def generar_contenido_html(titulo, contenido_videos):
    # ... (Set up HTML structure)
    with open("articulo_seo_python.html", "w", encoding="utf-8") as archivo_html:

Possible Uses and Customizations

The script is highly versatile, and with minor modifications, it can serve many different purposes. Here are some ideas:

  1. Increase the number of videos analyzed: Modify the maxResults parameter in the buscar_videos function.
  2. Customize the GPT-3 prompt to reflect different content styles or output requirements.
  3. Use different language models or update the temperature parameter to change the output’s randomness.
  4. Scrape video metadata, such as the number of views, likes, comments, or keywords, and incorporate them into the article.
  5. Improve the content’s structure and formatting to suit your preferences or platform requirements (e.g., changing headings, adding images, or enhancing the visual presentation).

Overall, this script presents a solid foundation for generating SEO-optimized content using the YouTube API and OpenAI GPT-3. By tweaking the code, you can tailor it to suit your specific needs and preferences, as well as accommodate other content types or sources. With some creativity and customization, you can leverage this script to create highly engaging, informative, and SEO-friendly content for your website, blog, or other online platforms.