Open In App

Python – Obtain title, views and likes of YouTube video using BeautifulSoup

Last Updated : 29 Dec, 2020
Like Article

In this article, we will learn how can we obtain data (like title, views, likes, dislikes etc) from any YouTube video using a Python script. For this task, we are going to use very famous library for web scraping BeautifulSoup and Requests.

Modules required and Installation :

Requests :
Requests allows you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs.

pip install requests

Beautiful Soup:
Beautiful Soup is a library that makes it easy to scrape information from web pages. It sits atop an HTML or XML parser, providing Pythonic idioms for iterating, searching, and modifying the parse tree.

pip install beautifulsoup4

For a given URL of video, data scraping will be done. Then parsing of data (title, views, likes element) will be done using find() method of Beautiful Soup. It will find and store the values in the dictionary.

Code :

# importing the libraries
from bs4 import BeautifulSoup
import requests
# creating function
def scrape_info(url):
    # getting the request from url
    r = requests.get(url)
    # converting the text
    s = BeautifulSoup(r.text, "html.parser")
    # finding meta info for title
    title = s.find("span", class_="watch-title").text.replace("\n", "")
    # finding meta info for views
    views = s.find("div", class_="watch-view-count").text
    # finding meta info for likes
    likes = s.find("span", class_="like-button-renderer").span.button.text
    # saving this data in dictionary
    data = {'title':title, 'views':views, 'likes':likes}
    # returning the dictionary
    return data
# main function
if __name__ == "__main__":
    # URL of the video
    # calling the function
    data = scrape_info(url)
    # printing the dictionary


{‘title’: ‘ Placement100 | GeeksforGeeks ‘, ‘views’: ’18, 964 views’, ‘likes’: ’37’}

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads