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 :
from bs4 import BeautifulSoup
import requests
def scrape_info(url):
r = requests.get(url)
s = BeautifulSoup(r.text, "html.parser" )
title = s.find( "span" , class_ = "watch-title" ).text.replace( "\n" , "")
views = s.find( "div" , class_ = "watch-view-count" ).text
likes = s.find( "span" , class_ = "like-button-renderer" ).span.button.text
data = { 'title' :title, 'views' :views, 'likes' :likes}
return data
if __name__ = = "__main__" :
data = scrape_info(url)
print (data)
|
Output:
{‘title’: ‘ Placement100 | GeeksforGeeks ‘, ‘views’: ’18, 964 views’, ‘likes’: ’37’}
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Dec, 2020
Like Article
Save Article