Skip to content
Related Articles

Related Articles

Improve Article
Python – Obtain title, views and likes of YouTube video using BeautifulSoup
  • Last Updated : 29 Dec, 2020

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
    print(data)

Output:

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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :