1

I am trying to find the total number of views of all videos in a playlist I own in YouTube. I can see that a "total views" number is posted under the playlist name. However this appears to be the number of views generated by entering the playlist first. When I add up the number of views of individual videos I get a substantially larger number because, I assume, most of videos are watched by following a link other than the playlist.

In particular using the following JS in the YT console

ytInitialData.header.playlistHeaderRenderer.viewCountText.simpleText

just gives the same number that is available under the playlist name.

ChatGPT gives the following JS

var totalViews = 0;
var videos = document.querySelectorAll('ytd-playlist-video-renderer');
for (var i = 0; i < videos.length; i++) {
  var viewsElement = videos[i].querySelector('#metadata > span:nth- child(1)');
  if (viewsElement !== null) {
     var views = viewsElement.textContent.trim().replace(/\D/g,'');
     totalViews += parseInt(views);
  }
 }
 console.log('Total views of all videos in the playlist: ' + totalViews);

But this gives a total count of 0.

Edit:

A solution.

4
  • 1
    As you stated, adding the individual views for each video in a playlist will equal the combined total for all videos in the playlist. If you are looking for a coded solution that is unfortunately off-topic for Web Applications. You could look at the HTML after clicking on a "View full playlist" link and parse out the video titles and view counts based on the tag names, but again that is off-topic. Maybe try stack-overflow or reformat your question to fit this site.
    – Blindspots
    Commented Apr 11, 2023 at 20:02
  • @BlindSpots Did you mean that adding the individual views will NOT equal the given playlist view number?
    – Maesumi
    Commented Apr 12, 2023 at 6:18
  • Yes, you indicated that you are not interested in total Playlist views but rather total views of all videos that are in a particular playlist. I pointed out the info is displayed for scraping.
    – Blindspots
    Commented Apr 12, 2023 at 16:46
  • I'll link to a solution later tonight.
    – Blindspots
    Commented Apr 13, 2023 at 0:31

0

Browse other questions tagged or ask your own question.