Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Pafy – Getting Comment Number for each item of Playlist

  • Last Updated : 28 Jul, 2020

In this article we will see how we can get number of comments of each item of playlist in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A playlist in YouTube is a list, or group, of videos that plays in order, one video after the other. Comments are the messages send via viewers on the video, they are public i.e can be viewed by everyone. Also owner has the option to disable the comments.

We can get the playlist from youtube in pafy with the help of get_playlist method, below is the command given to do this

 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

 pafy.get_playlist(url)

The playlist url should exist on youtube as it get the information of those videos which are present on the youtube. YouTube is an American online video-sharing platform.



Steps for implementation
1. Import the pafy module
2. Get the playlist with the help of URL of playlist
3. Return play list work as dictionary so use ‘items’ as key with the return playlist
4. Store the result in variable and from the items select the single item
5. With the single item use ‘playlist_meta’ key with it to get the meta data
6. With the meta data use ‘comments’ key this will return number of comments for given item

Below is the implementation




# importing pafy
import pafy 
    
# url of playlist
url = "https://www.youtube.com / playlist?list = PLqM7alHXFySGqCvcwfqqMrteqWukz9ZoE"
  
# getting playlist
playlist = pafy.get_playlist(url)
  
# getting playlist items
items = playlist["items"]
  
# selecting single item
item = items[1]
  
# getting pafy object
i_pafy = item['pafy']
      
# getting meta data
metadata = item['playlist_meta']
  
  
# getting number of comments of item
value = metadata['comments']
  
# printing value
print(value)

Output :

116

Another example




# importing pafy
import pafy 
    
# url of playlist
url = "https://www.youtube.com / playlist?list = PLqM7alHXFySE71A2bQdYp37vYr0aReknt"
    
# getting playlist
playlist = pafy.get_playlist(url)
  
# getting playlist items
items = playlist["items"]
  
# selecting single item
item = items[2]
  
# getting meta data
metadata = item['playlist_meta']
  
# getting number of comments of item
value = metadata['comments']
  
# printing value
print(value)

Output :

24



My Personal Notes arrow_drop_up
Recommended Articles
Page :