Simple script for downloading Youtube comments without using the Youtube API
npm install youtube-comment-downloader一个十分简单的脚本,用于下载 YouTube 评论而无需使用 YouTube API。输出格式为行分隔的 JSON。
egbertbouman/youtube-comment-downloader 的 Node.js/TypeScript 移植版本,并添加了代理支持。
English | 中文
通过 pnpm 安装此包(推荐):
``bash`
pnpm add youtube-comment-downloader
或使用 npm:
`bash`
npm install youtube-comment-downloader
全局 CLI 使用:
`bash`
pnpm add -g youtube-comment-downloader或
npm install -g youtube-comment-downloader
`bash
$ youtube-comment-downloader --help
Usage: youtube-comment-downloader [options]
下载 YouTube 评论而无需使用 YouTube API
Options:
-y, --youtubeid
-u, --url
-o, --output
-p, --pretty 将输出格式更改为缩进的 JSON
-l, --limit
-a, --language
--proxy
-s, --sort
-h, --help 显示帮助信息
`
使用视频 URL 下载评论:
`bash`
youtube-comment-downloader --url https://www.youtube.com/watch?v=ScMzIvxBSi4 --output ScMzIvxBSi4.json
使用视频 ID 下载评论:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json
使用美化格式下载:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --pretty
仅下载前 100 条评论:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --limit 100
下载热门评论而非最新评论:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --sort 0
使用代理下载:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --proxy http://proxy.example.com:8080
使用带身份验证的代理下载:
`bash`
youtube-comment-downloader --youtubeid ScMzIvxBSi4 --output ScMzIvxBSi4.json --proxy http://username:password@proxy.example.com:8080
对于以 -(破折号)开头的 YouTube ID,使用 = 语法:
`bash`
youtube-comment-downloader --youtubeid=-idwithdash --output output.json
您也可以在 Node.js/TypeScript 项目中以编程方式使用此包:
`javascript
const { YoutubeCommentDownloader, SORT_BY_POPULAR } = require('youtube-comment-downloader');
async function downloadComments() {
const downloader = new YoutubeCommentDownloader();
// 从 URL 下载评论
const comments = downloader.getCommentsFromUrl(
'https://www.youtube.com/watch?v=ScMzIvxBSi4',
SORT_BY_POPULAR
);
// 打印前 10 条评论
let count = 0;
for await (const comment of comments) {
console.log(comment);
if (++count >= 10) break;
}
}
downloadComments();
`
`typescript
import { YoutubeCommentDownloader, SORT_BY_POPULAR, Comment } from 'youtube-comment-downloader';
async function downloadComments(): Promise
const downloader = new YoutubeCommentDownloader();
// 从视频 ID 下载评论
const comments = downloader.getComments('ScMzIvxBSi4', SORT_BY_POPULAR);
// 收集所有评论
const allComments: Comment[] = [];
for await (const comment of comments) {
allComments.push(comment);
}
console.log(下载了 ${allComments.length} 条评论);
}
downloadComments();
`
`typescript
import { YoutubeCommentDownloader, SORT_BY_RECENT, Comment } from 'youtube-comment-downloader';
async function downloadWithProxy(): Promise
// 创建带代理配置的下载器
const downloader = new YoutubeCommentDownloader({
proxy: { uri: 'http://proxy.example.com:8080' }
});
// 使用代理下载评论
const comments = downloader.getComments('ScMzIvxBSi4', SORT_BY_RECENT);
for await (const comment of comments) {
console.log(${comment.author}: ${comment.text});
}
}
downloadWithProxy();
`
`javascript
const { YoutubeCommentDownloader, SORT_BY_RECENT } = require('youtube-comment-downloader');
async function downloadWithLanguage() {
const downloader = new YoutubeCommentDownloader();
// 使用中文语言偏好下载评论
const comments = downloader.getComments(
'ScMzIvxBSi4',
SORT_BY_RECENT,
'zh-CN' // 语言代码
);
for await (const comment of comments) {
console.log(${comment.author}: ${comment.text});`
}
}
用于下载 YouTube 评论的主类。
#### 构造函数
##### new YoutubeCommentDownloader(options?)
创建 YouTube 评论下载器的新实例。
- options (DownloaderOptions, 可选): 配置选项proxy
- (ProxyConfig, 可选): 代理配置uri
- (string): 代理 URI(例如 http://proxy.example.com:8080 或 http://user:pass@proxy.example.com:8080)
#### 方法
##### getComments(youtubeId, sortBy?, language?, sleepTime?)
通过视频 ID 下载 YouTube 视频的评论。
- youtubeId (string): YouTube 视频 IDsortBy
- (number, 可选): 排序顺序 - SORT_BY_RECENT (1) 或 SORT_BY_POPULAR (0)。默认:SORT_BY_RECENTlanguage
- (string, 可选): YouTube 生成文本的语言代码(例如 'zh-CN', 'en', 'ja')sleepTime
- (number, 可选): 请求之间的休眠时间(秒)。默认:0.1
返回:AsyncGenerator - 产生评论对象的异步生成器
##### getCommentsFromUrl(youtubeUrl, sortBy?, language?, sleepTime?)
通过 URL 下载 YouTube 视频的评论。
- youtubeUrl (string): 完整的 YouTube 视频 URLsortBy
- (number, 可选): 排序顺序 - SORT_BY_RECENT (1) 或 SORT_BY_POPULAR (0)。默认:SORT_BY_RECENTlanguage
- (string, 可选): YouTube 生成文本的语言代码sleepTime
- (number, 可选): 请求之间的休眠时间(秒)。默认:0.1
返回:AsyncGenerator - 产生评论对象的异步生成器
生成器产生的每个评论都具有以下结构:
`typescript`
interface Comment {
cid: string; // 评论 ID
text: string; // 评论文本内容
time: string; // 时间字符串(例如 "2小时前")
author: string; // 评论作者的显示名称
channel: string; // 作者的频道 ID
votes: string; // 点赞数
replies: number; // 回复数
photo: string; // 作者头像 URL
heart: boolean; // 评论是否收到视频创作者的爱心
reply: boolean; // 这是否是对另一条评论的回复
time_parsed?: number; // Unix 时间戳(如果可解析)
paid?: string; // 付费评论/超级聊天消息(如果适用)
}
- SORT_BY_POPULAR (0): 按热门程度排序评论SORT_BY_RECENT
- (1): 按最新程度排序评论(最新优先)
- 无需 YouTube API 密钥
- 支持视频 URL 和视频 ID
- 下载所有评论包括回复
- 按热门或最新排序
- 语言偏好支持
- 代理支持 - 使用 HTTP/HTTPS 代理改善访问
- 失败时自动重试
- 自动处理同意页面
- 支持付费评论/超级聊天
- 行分隔 JSON 或美化格式输出
- 完整的 TypeScript 支持
`bash克隆仓库
git clone https://github.com/p697/node-youtube-comment-downloader.git
cd node-youtube-comment-downloader
$3
`bash
运行所有测试
pnpm test以监视模式运行测试
pnpm run test:watch运行带覆盖率的测试
pnpm test -- --coverage
``MIT
欢迎贡献!请随时提交 Pull Request。
此包仅用于教育目的。使用此工具时,请尊重 YouTube 的服务条款以及内容创作者和评论者的权利。