Extracting the playlist name for each video directly from the HTML of a YouTube channel page can be challenging, especially if you're trying to do so without using the YouTube API or external tools. YouTube's page structure and dynamic content loading can vary, making it difficult to get consistent results solely based on DOM manipulation, particularly since playlist information might not be directly available in the same context as the video links.
However, here's a general approach you could try to extend your existing script. This assumes that the playlist information is available somewhere on the page where each video is listed (which might not always be the case depending on YouTube's page layout changes):
(function() {
// Select all video links
const videoLinks = document.querySelectorAll('a#video-title-link');
// Store extracted data
let videos = [];
// Iterate through video links and extract details
videoLinks.forEach(link => {
let title = link.textContent.trim();
let url = link.href;
// Attempt to find a parent element that might contain playlist info
let playlistName = '';
const playlistElement = link.closest('div')?.querySelector('.playlist-name-class'); // Replace '.playlist-name-class' with actual class/selector
if (playlistElement) {
playlistName = playlistElement.textContent.trim();
}
// Push the video data to the array
videos.push({ title, url, playlistName });
});
// Log the output to the console
console.clear();
console.table(videos);
})();
Key Considerations:
-
Dynamic Page Content: You might need to account for content that loads dynamically as you scroll or interact with the page. This would require additional scripting, such as listening to scroll events.
-
Selectors: The selector '.playlist-name-class' is just a placeholder. You need to inspect the page source to find the correct selector where playlist names are stored, if they are accessible.
-
Page Structure Changes: YouTube frequently updates its layout and internal structure, so this script can break if YouTube changes how they display videos and associated metadata.
-
Legal and Ethical Considerations: Scraping web pages might violate YouTube's terms of service. Ensure you understand and comply with any legal or ethical guidelines when attempting to scrape data from websites.
Due to these challenges, if you consistently need this information, consider using the YouTube Data API which provides a more stable and reliable way to access detailed data including playlist information, albeit with the requirement of an API key and applicable usage limits.