TikTokApi Python Wrapper

repository·main·Indexed 27 days ago

https://github.com/davidteather/tiktok-api

An unofficial Python wrapper for TikTok.com designed for data retrieval, such as trending data and specific user information. It requires Python 3.9+ and Playwright. The library is intended for data retrieval only and does not support posting content or authenticated user routes.

Tokens
1.5K
Snippets
6
Records
11
Agent score
42%

What's inside TikTokApi

  1. Install TikTokApi using Docker

    main

    If you prefer using Docker, you can build an image based on the Playwright focal image. This assumes your script (e.g., your_script.py) is located in the root of the repository directory.

    docker pull mcr.microsoft.com/playwright:focal
    docker build . -t tiktokapi:latest
    docker run -v TikTokApi --rm tiktokapi:latest python3 your_script.py
  2. Install TikTokApi via Docker

    main

    You can run the API using Docker by pulling the Playwright image, building the local repository, and running your script. Note that the following example assumes your script is named your_script.py and is located in the repository root.

    docker pull mcr.microsoft.com/playwright:focal
    docker build . -t tiktokapi:latest
    docker run -v TikTokApi --rm tiktokapi:latest python3 your_script.py
    docker pull mcr.microsoft.com/playwright:focal
    docker build . -t tiktokapi:latest
    docker run -v TikTokApi --rm tiktokapi:latest python3 your_script.py
  3. Install TikTokApi via pip

    main

    To install the TikTokApi package, you must have Python 3.9 or higher installed. You also need to install the Playwright browsers used by the library for scraping.

    Run the following commands in your terminal:

    pip install TikTokApi
    python -m playwright install
  4. Install TikTokApi

    main

    To use this library, you need Python 3.9 or higher. Install the package via pip and ensure the Playwright browsers are installed by running the following commands:

    pip install TikTokApi
    python -m playwright install
    pip install TikTokApi
    python -m playwright install
  5. Troubleshoot TikTokApi common issues

    main

    EmptyResponseException

    This error indicates that TikTok has intercepted the request and detected a bot. This is often due to environment settings. Using a proxy (residential or datacenter) may be required for stable scraping.

    Browser has no attribute ...

    Ensure you have installed the Playwright browsers by running: python3 -m playwright install.

    API methods return a Coroutine

    Many methods in this library are asynchronous. Ensure you use the await keyword when calling these methods within an async function.

  6. Troubleshoot EmptyResponseException

    main
    An EmptyResponseException indicates that TikTok is blocking the request and detecting the client as a bot. To resolve this, you may need to use a proxy. Residential proxies are often more successful than data center IPs for bypassing these blocks.
  7. Fetch trending TikTok videos

    main

    Use the TikTokApi class to fetch trending videos. Note that many methods are asynchronous and must be awaited. It is recommended to provide an ms_token (retrieved from your cookies on tiktok.com) to improve request success rates.

    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, browser=os.getenv("TIKTOK_BROWSER", "chromium"))
            async for video in api.trending.videos(count=30):
                print(video)
                print(video.as_dict)
    
    if __name__ == "__main__":
        asyncio.run(trending_videos())
  8. Get trending videos with TikTokApi

    main

    This example demonstrates how to fetch the latest trending videos using TikTokApi.

    Requirements:

    • You should retrieve an ms_token from your browser cookies on tiktok.com and provide it via an environment variable or directly in the script.
    • The library uses asynchronous methods; ensure you use await and run within an asyncio loop.
    • You can specify the browser type using the TIKTOK_BROWSER environment variable (defaults to chromium).

    To run the provided example script directly from the repository root:

    python -m examples.trending_example
    from TikTokApi import TikTokApi
    import asyncio
    import os
    
    ms_token = os.environ.get("ms_token", None) # 从 tiktok.com 的浏览器 Cookie 中获取你的 ms_token
    
    async def trending_videos():
        async with TikTokApi() as api:
            await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, browser=os.getenv("TIKTOK_BROWSER", "chromium"))
            async for video in api.trending.videos(count=30):
                print(video)
                print(video.as_dict)
    
    if __name__ == "__main__":
        asyncio.run(trending_videos())
  9. Access full data via .as_dict

    main
    To access the complete raw data dictionary for a TikTok object (such as a video), use the .as_dict property. Note that TikTok's data structure changes frequently, so it is recommended to inspect the dictionary structure during development.