B站评论删除 API 逆向分析:3 个关键参数 (oid, type, csrf) 获取与实战
B站评论删除API逆向工程实战关键参数解析与自动化操作指南1. 理解B站评论删除机制的核心逻辑在视频平台内容管理中评论删除功能涉及复杂的权限验证和数据交互流程。B站的评论删除API采用了一套基于Web安全标准的防护机制主要依赖三个关键参数实现身份验证和操作授权oid目标内容的对象标识符相当于数据库中的主键type内容类型分类代码决定API路由路径csrf跨站请求伪造令牌用于防止未授权操作通过Chrome开发者工具分析网络请求时可以看到典型的删除请求结构如下POST /x/v2/reply/del HTTP/1.1 Host: api.bilibili.com Content-Type: application/x-www-form-urlencoded oid123456789type1rpid987654321csrfabcdef0123456789注意实际操作中必须使用已登录状态的Cookie否则csrf验证会失败。每个用户的csrf令牌具有唯一性和时效性。2. 关键参数获取方法论2.1 oid参数的定位技巧oidObject ID根据内容类型不同其获取方式存在差异内容类型oid来源示例值范围视频视频AV号或BV号转换8-10位数字专栏文章文章cv编号7-9位数字动态动态did11位数字音频音频au编号6-8位数字在网页端可通过以下JavaScript代码快速获取当前页面的oid// 视频页面 window.__INITIAL_STATE__.aid // 动态页面 window.__INITIAL_STATE__.dynamic.id2.2 type参数枚举表type参数采用固定数值对应不同内容类型数值内容类型典型应用场景1视频主站视频评论11专栏文章评论区17动态用户动态互动14音频音乐区内容评论12课程课堂类内容2.3 csrf令牌的三种获取方式Cookie提取法document.cookie.match(/bili_jct([^;])/)[1]页面元素提取# 使用BeautifulSoup解析 soup.find(meta, {name: csrf_token})[content]API响应提取curl -s https://api.bilibili.com/x/web-interface/nav | jq .data.token3. 实战操作构建自动化删除工具3.1 浏览器开发者工具操作流程打开目标内容页面视频/动态/专栏右键点击评论 → 选择检查切换到Network面板 → 勾选Preserve log执行删除操作 → 观察新增的POST请求复制请求中的Form Data参数3.2 Python自动化脚本示例import requests def delete_comment(oid, rpid, type_, csrf, cookie): url https://api.bilibili.com/x/v2/reply/del headers { User-Agent: Mozilla/5.0, Cookie: cookie } data { oid: oid, type: type_, rpid: rpid, csrf: csrf } response requests.post(url, headersheaders, datadata) return response.json() # 示例调用 result delete_comment( oid12345678, rpid56789012, type_1, csrfabcdef123456, cookieSESSDATAxxxxxx; bili_jctyyyyyy ) print(result)提示实际使用时需要替换为真实的参数值和Cookie信息建议通过环境变量管理敏感数据。4. 安全机制与反爬策略应对B站的API防护体系主要包含以下安全层请求频率限制单IP每分钟不超过60次请求相同操作间隔需大于5秒签名验证关键参数需要MD5哈希校验时间戳参与签名计算行为验证异常操作触发Geetest验证账号行为模式分析应对建议添加随机延迟3-10秒使用真实浏览器UA头避免集中批量操作5. 高级应用评论管理系统设计对于需要管理大量内容的UP主可以构建基于Flask的Web管理界面from flask import Flask, request import sqlite3 app Flask(__name__) app.route(/manage, methods[POST]) def manage_comments(): conn sqlite3.connect(comments.db) c conn.cursor() # 获取待处理评论列表 c.execute(SELECT * FROM pending_comments WHERE status0) comments c.fetchall() for comment in comments: result delete_comment( oidcomment[1], rpidcomment[2], type_comment[3], csrfrequest.form[csrf], cookierequest.form[cookie] ) if result[code] 0: c.execute(UPDATE pending_comments SET status1 WHERE id?, (comment[0],)) conn.commit() conn.close() return {status: success}配套数据库schema设计CREATE TABLE pending_comments ( id INTEGER PRIMARY KEY, oid INTEGER NOT NULL, rpid INTEGER NOT NULL, type INTEGER NOT NULL, content TEXT, post_time DATETIME, status INTEGER DEFAULT 0 );6. 异常处理与日志记录完善的错误处理机制应包含以下组件重试机制from tenacity import retry, stop_after_attempt, wait_exponential retry(stopstop_after_attempt(3), waitwait_exponential(multiplier1, min4, max10)) def safe_delete(oid, rpid, type_, csrf, cookie): return delete_comment(oid, rpid, type_, csrf, cookie)日志记录import logging logging.basicConfig( filenamecomment_mod.log, format%(asctime)s - %(levelname)s - %(message)s, levellogging.INFO ) try: result safe_delete(...) logging.info(fDeleted rpid:{rpid} oid:{oid}) except Exception as e: logging.error(fFailed to delete {rpid}: {str(e)})结果验证检查返回JSON中的code字段验证ttl值是否为1确认message内容7. 浏览器扩展开发方案通过Chrome扩展可以更便捷地获取页面参数// manifest.json { manifest_version: 3, name: B站评论管理助手, version: 1.0, permissions: [cookies, https://*.bilibili.com/*], action: { default_popup: popup.html }, content_scripts: [{ matches: [https://*.bilibili.com/*], js: [content.js] }] } // content.js chrome.runtime.onMessage.addListener((request, sender, sendResponse) { if (request.action getCommentInfo) { const oid window.__INITIAL_STATE__.aid; const csrf document.cookie.match(/bili_jct([^;])/)[1]; sendResponse({oid, csrf}); } });扩展功能模块设计当前页面参数自动获取评论列表可视化展示批量选择删除操作操作历史记录查询

相关新闻