davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.9k stars 982 forks source link

[BUG] - async for reply in comment.replies(count=10): replies It is always impossible to get the comment data of the reply #1181

Open renzaijianghu1 opened 3 months ago

renzaijianghu1 commented 3 months ago

from TikTokApi import TikTokApi import asyncio import os

video_id = 7379950165601307905 ms_token = os.environ.get("ms_token", None) # set your own ms_token

async def get_comments(): async with TikTokApi() as api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) video = api.video(id=video_id) api.trending count = 0

    # Get comments
    async for comment in video.comments(count=400):
        count += 1
        if comment.as_dict['reply_comment_total'] > 0:
            print('count:', count)
            try:
                async for reply in comment.replies(count=10):
                    print("reply:", reply)
                    print("***1:", reply.id)
                    print("***2:", reply.parent)
                    print("***3::", reply.as_dict)
                    print("***4::", reply.likes_count)
                    print("***5::", reply.author)
                    print("***6::", reply.text)

            except Exception as e:
                print("Error retrieving replies:", e)

if name == "main": asyncio.run(get_comments())

Giaochan commented 3 months ago

Any news on this? No replies object is found in the comments object, this is the code im using from TikTokApi import TikTokApi import asyncio import os

video_id = 7248300636498890011 ms_token = os.environ.get("ms_token", None) # set your own ms_token

async def get_comments(): async with TikTokApi() as api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) video = api.video(id=video_id) count = 0

    async for comment in video.comments(count=30):
        print("Comment:")
        print(comment)
        print(comment.as_dict)

        # Fetch and print replies for the comment
        async for reply in comment.replies(count=10):  # Adjust count as needed
            print("  Reply:")
            print(reply)
            print(reply.as_dict)

if name == "main": asyncio.run(get_comments())

Giaochan commented 2 months ago

any news?

ocofon commented 1 month ago

One parameter to retrieve the replies is wrong. https://github.com/davidteather/TikTok-Api/blob/a4079f0a7ccac4f2a7482272f028849b45387a7d/TikTokApi/api/comment.py#L61

item_id should be the video id and not the user id.

"item_id": self.as_dict["aweme_id"],

You can workaroud this by adding comment.author.user_id = comment.as_dict["aweme_id"] right before the comment.replies() call.

e.g.:

        comment.author.user_id = comment.as_dict["aweme_id"]
        async for reply in comment.replies(count=10):
            print("  Reply:")
            print(reply)
            print(reply.as_dict)