To achieve this task, you'll need to identify each video’s associated playlists directly from the channel page. However, this requires accessing elements that contain playlist information, which might not be explicitly linked on the main channel page without interacting with the YouTube API or navigating to individual video pages.
Nonetheless, if you want to continue in a no-API fashion, any available playlist information would typically require scraping it from sections that show playlists directly related to videos on the current page. If YouTube's page structure includes such details, you can scrape it.
Below is a modified version that assumes playlist information is available alongside each video link. This is purely hypothetical because, in practice, extracting such information depends on how and if it's rendered on YouTube's page source. Since direct access on the main page is usually not available, this serves as a conceptual outline, assuming HTML structure supports it:
(function() {
// Select all video elements that contain both titles and associated playlist information
const videoElements = document.querySelectorAll('.video-element'); // This selector should be replaced with the correct one if any
// Store extracted data
let videos = [];
// Iterate through video elements to extract video details and associated playlist names
videoElements.forEach(videoElement => {
let titleLink = videoElement.querySelector('a#video-title-link');
let title = titleLink ? titleLink.textContent.trim() : 'Untitled';
let url = titleLink ? titleLink.href : '';
// Assume there's an element or elements that contain associated playlists
let playlistsArray = [];
let playlistElements = videoElement.querySelectorAll('.playlist-info'); // Update with actual class or id
playlistElements.forEach(playlist => {
playlistsArray.push(playlist.textContent.trim());
});
// Create a string from playlists array, separating them by commas
let playlistsString = playlistsArray.join(', ');
// Push the video data with playlists to the array
videos.push({title, url, playlists: playlistsString});
});
// Log the output to the console
console.clear();
console.table(videos);
})();
Important Notes:
-
Selector accuracy: The selectors like #video-title-link and .playlist-info are placeholders. You will need to inspect the actual HTML structure and identify the correct selectors where playlist information might be accessible.
-
Complexity and Feasibility: Unfortunately, without programmatically traversing elements on YouTube or reviewing its structure by loading individual video pages, obtaining direct playlist associations might not be feasible from just the channel page.
-
Dynamic Content: Remember, YouTube pages often use scripts to dynamically load content. Ensure you are inspecting the page with all content fully loaded (e.g., scrolling to populate data if using infinite scroll features).
-
Data Rendering: YouTube might not render playlist information on the channel or main video list page, emphasizing more the need for API solutions for reliable data extraction for videos and their playlists.