• author: Monkhaus

Building a Simple Backend and Frontend for Scraping and Generating Random Youtube Videos

In this article, we'll be exploring how to build a simple front-end and back-end to scrape YouTube and generate random YouTube videos. This article will be an extension of our previous video in which we scraped YouTube and generated random videos. However, this time we'll create a simple front-end, and we'll add a little back-end to serve that front-end.

Getting Started

Before we get started, I would like to mention that all the code is available on my Patreon, which you can join by paying one pound a month. The code is now available on my Patreon so that you can access it for free. All you need to do is join, and you will get access to the code.

To begin with, we will need to start our server by running the 'start.py' script, which automatically puts our server at the local host on port 8000. If you're on Linux using WSL, you will need to come to your file system, go to Linux, and find the path to where you have put the index file to watch videos. Here, we no longer need to click the link from the command line to watch videos.

Project Structure

Our project structure includes the following files:

  • 'api.py': Our API file
  • 'get_videos.py': File for getting videos
  • 'start.py': Used to start the server
  • 'requirements.txt': Contains all the necessary requirements for this to work
  • 'database.sqlite3': A database we created
  • '/static/index.html': Contains the front-end code

Back-End Code

Our back-end code is made up of our API, which is created using Fast API, and it is really simple. We will have one view, which is just a default view, and we don't necessarily need to have it. We also have a middleware that is necessary because we will be making requests from our front-end. We will need to allow requests from anywhere, which is what the middleware does, and we specify that using the CORS (Cross-Origin Resource Sharing) middleware.

We have the 'getrandomvideo' function which returns a random video URL from our videos table, which is created as part of our database. To get to this endpoint, we first need to get the videos which are collected using the 'get_videos.py' script.

Front-End Code

Our front-end code is quite simple, and it consists of an index.html file that is sitting in the static folder. This allows for a faster API project. Inside the index.html file, we have a button that grabs a random video from the API and displays it. The display is done by first removing the previous video and then appending the new video.

Improving the Code

As with any code, there is always room for improvement. We can improve the code we have in a variety of ways that include, but are not limited to:

  • The ability to post the channel we are interested in
  • Getting multiple channels
  • Syncing the videos we get into our database
  • Making sure we do not scrape public web content thousands of times.


The code might seem complicated, but it is relatively simple, and all the code is available on my Patreon for free. By going through this tutorial, you should now be able to scrape YouTube, generate random videos, and serve them on a front-end. However, it is essential to keep in mind that scraping public web content thousands of times might lead to your IP being blocked. Therefore, it's recommended to use a web scraping proxy or a VPN to change your IP.

Previous Post

Building a Text-Based Hacker Game: The Broken Access Control Level

Next Post

Converting a Python To-Do List Command Line App to Use Sqlite3 for Storage

About The auther

New Posts

Popular Post