Skip to content

[BUG] 最多获取50条评论 #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shuwu-ui opened this issue Apr 23, 2025 · 1 comment
Open

[BUG] 最多获取50条评论 #613

shuwu-ui opened this issue Apr 23, 2025 · 1 comment
Assignees
Labels
BUG Something isn't working

Comments

@shuwu-ui
Copy link

import requests
import json
from datetime import datetime

def fetch_douyin_comments(aweme_id: str, cursor: int = 0, count: int = 5000) -> dict:
    """
    调用抖音评论获取API
    
    参数:
        aweme_id: 视频ID
        cursor: 分页游标
        count: 获取数量
    """
    url = f"https://api.douyin.wtf/api/douyin/web/fetch_video_comments"
    
    params = {
        "aweme_id": aweme_id,
        "cursor": cursor,
        "count": count
    }
    
    try:
        response = requests.get(url, params=params)
        response.raise_for_status()  # 检查请求是否成功
        return response.json()
    except requests.exceptions.RequestException as e:
        print(f"请求失败: {e}")
        return None

def save_comments_to_json(data: dict, aweme_id: str):
    """
    将评论数据保存为JSON文件
    
    参数:
        data: 评论数据
        aweme_id: 视频ID
    """
    # 生成文件名,包含视频ID和时间戳
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    filename = f"comments_{aweme_id}_{timestamp}.json"
    
    # 保存数据
    try:
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        print(f"数据已保存到文件: {filename}")
    except Exception as e:
        print(f"保存文件失败: {e}")

def get_comments_sequentially(aweme_id: str):
    """
    依次获取视频的评论内容
    
    参数:
        aweme_id: 视频ID
    """
    cursor = 0
    has_more = True
    total_comments = 0
    
    while has_more:
        result = fetch_douyin_comments(aweme_id, cursor)
        if not result or 'data' not in result:
            print("获取评论失败或没有更多评论")
            break
            
        comments = result['data'].get('comments', [])
        if not comments:
            print("没有更多评论了")
            break
            
        for comment in comments:
            total_comments += 1
            text = comment.get('text', '')
            nickname = comment.get('user', {}).get('nickname', '未知用户')
            create_time = datetime.fromtimestamp(comment.get('create_time', 0))
            print(f"\n评论 #{total_comments}")
            print(f"用户: {nickname}")
            print(f"时间: {create_time.strftime('%Y-%m-%d %H:%M:%S')}")
            print(f"内容: {text}")
            
        cursor = result['data'].get('cursor', 0)
        has_more = result['data'].get('has_more', False)
        
    print(f"\n总共获取到 {total_comments} 条评论")
    return total_comments

if __name__ == "__main__":
    # 使用示例
    video_id = input("请输入视频ID: ")
    print(f"开始获取视频 {video_id} 的评论...")
    total = get_comments_sequentially(video_id)
    
    # 询问是否保存评论
    if total > 0:
        save = input("\n是否要保存评论数据到文件?(y/n): ")
        if save.lower() == 'y':
            result = fetch_douyin_comments(video_id, count=total)
            if result:
                save_comments_to_json(result, video_id)
                print("评论数据已保存完成!") 

请问一下是我代码的原因嘛?
我每次获取都只能获取50条,就没有了,我暂时测试使用的是demo网址

Image

@shuwu-ui shuwu-ui added the BUG Something isn't working label Apr 23, 2025
@shuwu-ui shuwu-ui changed the title [BUG] 简短明了的描述问题 [BUG] 最多获取50条评论 Apr 23, 2025
@Evil0ctal
Copy link
Owner

尝试一下自己部署 然后使用自己已登录的抖音cookie替换默认的cookie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants