怎样把指定抖音视频的评论原文导出给翻译或表格使用?
用 GET /v1/douyin/videos/comments/list 传该公开视频的 id 或 url,读取 data.items[].text。把每条评论的 id、post_id 与原文保留在一行,用 cursor 继续读取需要的页,翻译结果可放在单独一列。
DataXAPI 价目生效日期与版本 2026-11-25 · v22
每条评论保留原文和关联键
可输出 comment_id、post_id、text、published_at 和可用的 metrics.likes。以 comment ID 去重,同一作品用 post_id 关联;缺失发布时间或正文时按缺失值记录。
CSV 如果会在表格软件中打开,应按 UTF-8 写出。不要把 ID 当成数字;评论中的逗号、引号和换行交给可靠的 CSV 写入器处理。
对样本范围和排序做清楚的说明
评论接口单页最多 20 条。用 page.next_cursor 请求后续页,达到所需样本量或请求预算就停止。接口文档没有按点赞排序的查询选项;若要看高赞原话,应对本次取得的评论按可用点赞数排序。
把中文原文与自己的翻译分列保存。样本里观察到的热门反馈取决于已取回的评论范围,不能直接代表平台所有用户。
用关键词跑多作品抽样并导出两级评论
先搜索两页、取最多 3 个有效作品,分别读取两页一级评论。对每个作品只选一个父评论展开两页回复。本例最大请求量 2 + 6 + 6 = 14。DataXAPI 通道都计费时,搜索每页 USD 0.02,评论和回复每页 USD 0.002,合计 USD 0.064。回复只代表所选父评论。
保存为 comments.py,在设好关键词与两把 Key 后运行。CSV 保留输入归属、原始 parent_id/root_id、请求父评论 ID 和正文。receipt.json 记录游标、警告、错误和上游请求号;按本次预算和公开可见性说明覆盖。
import csv, json, os
from urllib.error import HTTPError
from urllib.parse import urlencode
from urllib.request import Request, urlopen
base = 'https://social2api.com'
headers = {'Authorization': 'Bearer ' + os.environ['SOCIAL2API_API_KEY'],
'X-DataXAPI-Key': os.environ['DATAXAPI_API_KEY']}
term = os.environ['SOCIAL2API_QUERY']
receipts = []
def pages(path, params, cap, limit=2):
cursor = None
for _ in range(limit):
query = {**params, **({'cursor': cursor} if cursor else {})}
request = Request(base + path + '?' + urlencode(query),
headers={**headers, 'X-Max-Price': cap})
try:
with urlopen(request, timeout=90) as response:
body = json.load(response)
except HTTPError as error:
receipts.append({'path': path, 'http': error.code, 'params': query})
return
paging = body.get('page') or {}
receipts.append({'path': path, 'params': query, 'page': paging,
'request_id': body.get('request_id'), 'warnings': body.get('warnings', [])})
yield from body['data']['items']
cursor = paging.get('next_cursor')
if not paging.get('has_more') or not cursor:
return
def export(name, rows):
fields = ['comment_id', 'post_id', 'parent_id', 'root_id', 'requested_parent_id', 'text', 'published_at']
with open(name, 'w', encoding='utf-8-sig', newline='') as file:
writer = csv.DictWriter(file, fields)
writer.writeheader()
writer.writerows(rows.values())
def record(item, post_id, requested_parent_id=None):
return {'comment_id': item.get('id'), 'post_id': post_id, 'parent_id': item.get('parent_id'),
'root_id': item.get('root_id'), 'requested_parent_id': requested_parent_id,
'text': item.get('text'), 'published_at': item.get('published_at')}
post_ids = list(dict.fromkeys(item['id'] for item in pages('/v1/douyin/videos/search', {'query': term}, '0.02') if item.get('id')))[:3]
top, children = {}, {}
for post_id in post_ids:
selected_parent = None
for item in pages('/v1/douyin/videos/comments/list', {'id': post_id}, '0.002'):
if item.get('id'):
top[post_id, item['id']] = record(item, post_id)
selected_parent = selected_parent or item['id']
if selected_parent:
for item in pages('/v1/douyin/comments/replies/list', {'id': selected_parent, 'post_id': post_id}, '0.002'):
if item.get('id'):
children[post_id, item['id']] = record(item, post_id, selected_parent)
export('comments.csv', top)
export('replies.csv', children)
with open('receipt.json', 'w', encoding='utf-8') as file:
json.dump(receipts, file, ensure_ascii=False, indent=2)