davidteather / TikTok-Api

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

[BUG] - Error encountered getting comments: TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response #1097

Open binaryplatitude opened 7 months ago

binaryplatitude commented 7 months ago

Describe the bug

Tiktok throwing a fit and not giving me my comments.

The buggy code

Please add any relevant code that is giving you unexpected results.

Preferably the smallest amount of code to reproduce the issue.

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=10):
            print(comment)
            print(comment.as_dict)

if __name__ == "__main__":
    asyncio.run(get_comments())

Expected behavior

Comments printed out

Error Trace (if any)

Put the error trace below if there's any error thrown.

Traceback (most recent call last):
File "c:\Users\user\Documents\Python Project Workspace\Tiktok Data\commentTester.py", line 23, in
asyncio.run(get_comments())
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\asyncio\base_events.py", line 649, in run_until_complete
return future.result()
File "c:\Users\user\Documents\Python Project Workspace\Tiktok Data\commentTester.py", line 17, in get_comments
async for comment in video.comments(count=10):
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\site-packages\TikTokApi\api\video.py", line 263, in comments
resp = await self.parent.make_request(
File "C:\Users\user\anaconda3\envs\scapersecondenv\lib\site-packages\TikTokApi\tiktok.py", line 430, in make_request
raise EmptyResponseException(result, "TikTok returned an empty response")
TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

Desktop (please complete the following information):

Additional context

Its an older bug sir, but it checks out.

algoprofit8 commented 7 months ago

Looks like api is broken, i tried manualy do request to get user info https://www.tiktok.com/api/user/detail/?uniqueId=therock&msToken=token and its really return empty response

Xarbenence commented 7 months ago

I am getting this same exception on Mac Vetura 13.3 with the basic "trending videos" example.

`from TikTokApi import TikTokApi import asyncio import os

ms_token = os.environ.get("ms_token", None) # get your own ms_token from your cookies on tiktok.com

async def trending_videos(): async with TikTokApi() as api: await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) async for video in api.trending.videos(count=30): print(video) print(video.as_dict)

if name == "main": asyncio.run(trending_videos())`

Traceback:

/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:34: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020 warnings.warn( Traceback (most recent call last): File "/Users/alexsharper/Documents/Lancing/Etai/TikTokBot/ex1.py", line 15, in <module> asyncio.run(trending_videos()) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete return future.result() File "/Users/alexsharper/Documents/Lancing/Etai/TikTokBot/ex1.py", line 10, in trending_videos async for video in api.trending.videos(count=30): File "/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/TikTokApi/api/trending.py", line 43, in videos resp = await Trending.parent.make_request( File "/Users/alexsharper/Library/Python/3.9/lib/python/site-packages/TikTokApi/tiktok.py", line 430, in make_request raise EmptyResponseException(result, "TikTok returned an empty response") TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response

gabrielrosendo commented 6 months ago

Any updates on this? Running the basic example and I was able to get a response once but after that I get "TikTok returned an empty response"

Xarenn commented 6 months ago

I have the same issue...

dungdl commented 6 months ago

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

Xarenn commented 6 months ago

Nope, for comments still doesn't work

VitoLin commented 6 months ago

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

This can also be fixed by setting the user_agent and viewport in the context_options while remaining headless

import asyncio
import os

ms_token = os.environ.get(
    "ms_token", None
) 
context_options = {
    'viewport' : { 'width': 1280, 'height': 1024 },
    'user_agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}

async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, context_options=context_options)
        async for video in api.trending.videos(count=30):
            print(video)
            print(video.as_dict)

if __name__ == "__main__":
    asyncio.run(trending_videos())

This also seems to bypass the need to have a ms_token

calvin5walters commented 6 months ago

@VitoLin This worked for me for a few runs, then it went back to the "EmptyResponseException: None -> TikTok returned an empty response" error. Any idea why it only works periodically? Is there a rate limit or something? Thanks

jpratt9 commented 5 months ago

Has anyone tried rotating the ms_token or proxy we go through? It might be bot/scraping-detection on TikTok's part.

calvin5walters commented 5 months ago

Has anyone tried rotating the ms_token or proxy we go through? It might be bot/scraping-detection on TikTok's part.

What do you mean by "rotating" the ms_token? And how would one do that? Thanks! @jpratt9

jpratt9 commented 5 months ago

I've tried this any it work: in the api.create_sessions you should add the param headless=False, this will open the playwright browser on your computer and then you can have the response. Full command:

api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, headless=False)

It's a bit annoying but preventing the Empty Response error

This can also be fixed by setting the user_agent and viewport in the context_options while remaining headless

import asyncio
import os

ms_token = os.environ.get(
    "ms_token", None
) 
context_options = {
    'viewport' : { 'width': 1280, 'height': 1024 },
    'user_agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
}

async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, context_options=context_options)
        async for video in api.trending.videos(count=30):
            print(video)
            print(video.as_dict)

if __name__ == "__main__":
    asyncio.run(trending_videos())

This also seems to bypass the need to have a ms_token

This only seems to work for me with api.trending, not api.user

carol-he commented 5 months ago

I'm still getting EmptyResponse no matter which one I try, even with headless=False

cloudengineer89 commented 5 months ago

Finally, I got a response. Maybe someone can help me out with headless=False. How not to show the pop-up window? Without this attribute, I got error: TikTokApi.exceptions.EmptyResponseException: None -> TikTok returned an empty response