使用Chrome控制台,先允许粘贴,手动把所有评论加载出来,然后使用下列代码,手动复制结果就好
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| (() => { // 选择评论的DOM元素,YouTube评论每条一般在 #contents 下的 ytd-comment-thread-renderer const commentElements = document.querySelectorAll('#contents ytd-comment-thread-renderer #content-text'); let comments = []; commentElements.forEach(el => { comments.push(el.innerText.trim()); }); // 将评论用换行符连接成一个字符串 const commentText = comments.join('\n\n----------\n\n'); // 打印输出,可以Ctrl+C复制 console.log(commentText); // 另外,可以创建下载链接便于直接保存TXT const blob = new Blob([commentText], { type: 'text/plain' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'youtube_comments.txt'; a.textContent = '点击这里下载评论文本文件'; document.body.appendChild(a); })();
|
如果你想展开回复,可以先执行下面的代码。该代码会2s检查一下有没有回复,然后点击。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| (() => { function expandAllReplies() { // 选取所有展开回复的按钮,涵盖 "more-replies" 以及页面上所有带有"reply/replies/回复"关键词的按钮 const buttons = Array.from(document.querySelectorAll('ytd-button-renderer#more-replies button'));
let clickedCount = 0; buttons.forEach(btn => { const label = btn.getAttribute('aria-label') || btn.innerText || ''; if (/reply|replies|回复/i.test(label.trim())) { btn.click(); clickedCount++; console.log(`点击展开回复按钮: ${label.trim()}`); } });
if (clickedCount > 0) { console.log(`本轮共点击展开 ${clickedCount} 个回复,2秒后继续检测...`); setTimeout(expandAllReplies, 2000); // 递归延迟2秒继续 } else { console.log('所有回复已展开完毕,无更多按钮可点击。'); } }
expandAllReplies(); })();
|
如果你认为上述代码影响你其他的操作,你可以全部加载后使用一次下面的代码展开回复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| (() => { // 选取当前页面所有“展开回复”按钮(数量不限),在对应的more-replies按钮内的<button>元素 const buttons = Array.from(document.querySelectorAll('ytd-button-renderer#more-replies button'));
let clickedCount = 0; buttons.forEach(btn => { const label = btn.getAttribute('aria-label') || btn.innerText || ''; if (/reply|replies|回复/i.test(label.trim())) { btn.click(); clickedCount++; console.log(`点击展开回复按钮: ${label.trim()}`); } });
if (clickedCount === 0) { console.log('当前没有找到可点击的展开回复按钮'); } else { console.log(`共点击展开 ${clickedCount} 个回复按钮`); } })();
|