Web Scraping is a specialized tool designed to quickly and accurately extract data from any web page. Web scraping tools vary greatly in terms of design and complexity, depending on the project. The need for web scraping is to collect data that can be useful in any way. In this article, I will take you through scraping youtube with Python. I will also show you how we can download any youtube video by using a one-line command.
Scraping Youtube with Python
For the task of scraping youtube with python, we have a package known as pytube3, which can be easily installed by using the pip command – pip install pytube3. Now let’s get started with the task of scraping youtube by importing this package:
from pytube import YouTube
You will notice that we have and installed pytube3 by using the pip command, but we are importing pytube and not pytube3. To eliminate confusion, pytube3 is also imported by writing only pytube. We don’t import it by writing as pytube3.
Our next step is to get the link of the youtube video which we need to scrape using python. Don’t forget to place your link between ” ” or ‘ ‘:
link = input("Enter Link of Youtube Video: ") yt = YouTube(link)
Now, as we have taken the link of a youtube video as an input, we will run the program while scraping some information about the video. Now, I will write some necessary functions that we can use for scraping youtube with python by using pytube module:
Code language: PHP (php)
# To print title print("Title :", yt.title) # To get number of views print("Views :", yt.views) # To get the length of video print("Duration :", yt.length) # To get description print("Description :", yt.description) # To get ratings print("Ratings :", yt.rating)
Now, when we’re going to run the above code, it will ask for input, so don’t forget to copy the link from the youtube video you want to scratch. Using the above functions, we will see various details about the video to which we introduced the link in the program. In addition, there are many other operations of this type that can be done that you can find in the official documentation pytube3.
So the output of our code above will look like this:
Enter Link of Youtube Video: " https://www.youtube.com/watch?v=2FbWhnTUebo "
Title : JEE, NEET update: What students feel about exams not getting postponed
Views : 13129
Duration : 142
Description : Students reacted to the government not postponing JEE, NEET exams this year. Students said that holding exams will increase the mental pressure. NEET and JEE exams are scheduled for September this year. The Government clarified that entrance exams will be held as scheduled. "Tentative dates for exams were in Nov, now scheduled for Sept. Now all the exams are scheduled for Sept, how will students prepare. UPSC is also happening at the same time, students will have to choose. The Government should have scheduled exams for/after November. There is a mental pressure after this sudden decision," said a student. Another student said, "A lot of exams happening in Sept, lots of subjects for students. I don’t think so much movement is good in this situation. If exams were held in November, it would have been better."
Ratings : 4.5597825
Downloading Youtube Video
The task of scraping youtube with python will be left incomplete if we do not know how to download the youtube video. It’s obvious that we are more considered about the data, but still, if you want to see how we can download the youtube video using python then you can use the code below:
Code language: PHP (php)
stream = yt.streams.get_highest_resolution() stream.download() print("Download completed!!")
The video will be downloaded and saved in the same directory where your python file is. I hope you liked this article on Scraping youtube with Python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning and Python.