从Youtube频道提取数据

问题描述 投票:0回答:1

我获得了从 Youtube 提取带有标题的视频列表及其链接的代码。它仅适用于视频,不适用于短片。代码如下:

代码1: var 滚动 = setInterval(function(){ window.scrollBy(0, 1000)}, 1000);

代码2: window.clearInterval(滚动);控制台.clear(); url = $$('a'); urls.forEach(function(v,i,a) {if (v.id=="视频标题链接") {console.log(' '+v.title+' '+v.href+' ')}}); 我希望代码能够提取包含标题、链接以及从 YouTube 上传的日期的短片列表。请提供。

我想从我的 YouTube 频道中提取我的 YouTube 短片的列表,其中包含标题、上传日期和链接。我上面提到的代码适用于 YouTube 视频,但不适用于 YouTube 短片。

list youtube extract
1个回答
0
投票

与常规视频相比,YouTube 短片具有不同的类别或属性。这是脚本的修改版本,适用于 YouTube 短片:

// Function to extract YouTube shorts information using async/await
const extractYouTubeShorts = async () => {
    try {
        // Scroll down to load all shorts (adjust the scrolling interval as needed)
        const scrollInterval = setInterval(() => {
            window.scrollBy(0, 1000);
        }, 1000);

        // Wait for enough time for the shorts to load
        await new Promise(resolve => setTimeout(resolve, 10000));

        clearInterval(scrollInterval);
        console.clear();

        // Select all anchor elements on the page
        const urls = document.querySelectorAll('a');

        // Iterate through each anchor element to find shorts
        urls.forEach(link => {
            // Check if it's a link to a short
            if (link.href.includes('/shorts/') && link.querySelector('#video-title')) {
                // Extract title and link
                const title = link.querySelector('#video-title').textContent.trim();
                const href = link.href;

                // Extract upload date if available (assuming it's nearby in DOM)
                const parentElement = link.closest('ytd-rich-grid-media');
                const dateString = parentElement.querySelector('#metadata-line span').textContent.trim(); // Adjust this selector based on your HTML structure

                // Log title, upload date, and link using template literals
                console.log(`${title}\t${dateString}\t${href}`);
            }
        });

    } catch (error) {
        console.error('Error extracting YouTube shorts:', error);
    }
};

// Call the function to extract YouTube shorts information
extractYouTubeShorts();
© www.soinside.com 2019 - 2024. All rights reserved.