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

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 :

filter_none

edit
close

play_arrow

link
brightness_4
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)

chevron_right


Output:

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.