You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改代码提取YouTube Shorts的标题、链接及上传日期?

提取YouTube频道Shorts信息的JS代码修改方案

原代码仅能提取YouTube普通视频的标题与链接,无法覆盖Shorts内容。以下是修改后的代码,可提取自己频道内Shorts的标题、链接及上传日期:

步骤1:自动滚动加载所有Shorts

执行这段代码会自动向下滚动页面,加载出频道内所有Shorts内容,直到页面底部自动停止:

var scroll = setInterval(function() {
    window.scrollBy(0, 1000);
    // 检测是否滚动至页面底部,到达则停止
    if ((window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
        clearInterval(scroll);
        console.log("所有Shorts内容已加载完成");
    }
}, 1000);

步骤2:提取Shorts的标题、链接与上传日期

待滚动加载完成后,执行这段代码提取并输出所有Shorts信息:

clearInterval(scroll);
console.clear();

// 获取所有Shorts卡片元素
const shortsCards = document.querySelectorAll('ytd-reel-item-renderer');

shortsCards.forEach((card, idx) => {
    // 提取Shorts链接
    const link = card.querySelector('a[href^="/shorts/"]');
    const shortUrl = link ? `https://www.youtube.com${link.getAttribute('href')}` : '链接获取失败';
    
    // 提取标题
    const title = card.querySelector('yt-formatted-string#video-title')?.textContent.trim() || '标题获取失败';
    
    // 提取上传日期
    const date = card.querySelector('span#metadata-line span:nth-child(2)')?.textContent.trim() || '日期获取失败';
    
    // 按序号、标题、链接、日期的格式输出,制表符分隔便于复制
    console.log(`${idx + 1}\t${title}\t${shortUrl}\t${date}`);
});

修改说明

  1. DOM选择器适配:Shorts在频道页面使用ytd-reel-item-renderer作为卡片容器,而非普通视频的结构,因此调整选择器定位Shorts元素
  2. 多信息提取:新增上传日期的提取逻辑,针对Shorts的元数据元素定位日期内容
  3. 滚动优化:自动检测页面底部,无需手动停止滚动,加载更高效

内容的提问来源于stack exchange,提问作者Fakhar Shafi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 23:14:57