How to Collect TikTok Data With Python

To collect TikTok data with Python, install apify-client, call apidojo's TikTok actors with your API token, and read each run's dataset.

Cover card reading 'How to Collect TikTok Data With Python'

To collect TikTok data with Python, install apify-client, call apidojo’s TikTok actors with your API token, and read each run’s dataset. One script can search videos by keyword, pull the comments on the best ones and save everything to CSV, with no TikTok login, proxies or developer approval.

This guide builds that script step by step: setup, a video search, chaining a comment scrape onto the results, cleaning the data and saving it. The actor choice for each data type is explained in how to scrape TikTok data in 2026.

What Do You Need to Collect TikTok Data With Python?

You need Python 3, the apify-client and pandas packages, and an Apify API token on a paid plan. apidojo’s TikTok actors run in demo mode on the free plan, 5 runs a month with 10 items each (Apify). Listed below are the 3 setup steps.

  1. Install the packages: pip install apify-client pandas.
  2. Copy your API token from Apify Console.
  3. Store it in an environment variable: export APIFY_TOKEN=your_token.

How Do You Search TikTok Videos With Python?

Call the TikTok Scraper actor with a keywords list and read the items from its dataset. The input and field names come from the actor’s documentation (Apify).

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

run = client.actor("apidojo/tiktok-scraper").call(run_input={
    "keywords": ["home workout"],
    "sortType": "MOST_LIKED",
    "location": "US",
    "maxItems": 500,
})
videos = list(client.dataset(run.default_dataset_id).iterate_items())
print(len(videos), "videos")

call() starts the run and waits until it finishes. Each item has id, title, views, likes, comments, shares, bookmarks, uploadedAtFormatted, postPage, a channel object and a song object.

How Do You Chain a Comment Scrape Onto Video Results?

Take the best videos from the first run and pass their postPage links to the TikTok Comments Scraper. This collects the conversation under the videos that matter, instead of every video.

top = sorted(videos, key=lambda v: v["views"], reverse=True)[:10]

run = client.actor("apidojo/tiktok-comments-scraper").call(run_input={
    "startUrls": [v["postPage"] for v in top if v["comments"] >= 10],
    "includeReplies": False,
    "maxItems": 5000,
})
comments = list(client.dataset(run.default_dataset_id).iterate_items())
print(len(comments), "comments")

The comments >= 10 check respects the Comments Scraper’s minimum of 10 comments per video (Apify). The comment fields are listed in how to scrape TikTok comments and replies.

How Do You Clean and Save TikTok Data in Python?

Load the results into pandas, drop duplicates on id, parse the dates, and write CSV or JSON Lines.

import pandas as pd

v = pd.json_normalize(videos).drop_duplicates("id")
v["uploaded"] = pd.to_datetime(v["uploadedAtFormatted"])
v["er"] = (v["likes"] + v["comments"] + v["shares"] + v["bookmarks"]) / v["views"] * 100
v[["id", "postPage", "uploaded", "channel.username", "views", "er", "title"]].to_csv("videos.csv", index=False)

c = pd.json_normalize(comments).drop_duplicates("id")
c.to_json("comments.jsonl", orient="records", lines=True, force_ascii=False)

Keep IDs as text in spreadsheets, because long numbers get rounded. The engagement rate formula is explained in how to calculate TikTok engagement rate.

How Do You Handle Errors and Limits?

Check each run’s status, keep inputs above the actors’ minimums, and split large jobs into smaller runs. Listed below are the 4 rules (Apify).

  1. Check run.status is SUCCEEDED before reading the dataset.
  2. Give each query or URL enough content: at least 10 posts on TikTok Scraper, 10 comments on Comments Scraper.
  3. Don’t pass single video URLs to TikTok Scraper; it accepts profile, hashtag, search, music and location URLs.
  4. Split very large runs into smaller batches.

More on limits and pagination is in TikTok scraping limits.

What Does This Script Cost to Run?

The example above, 500 videos and up to 5,000 comments, costs about $1.65: $0.15 for the videos and $1.50 for the comments. Both actors charge $0.30 per 1,000 items (Apify, Apify).

Questions people ask

Is there an official TikTok Python library?

TikTok's official APIs are called over HTTP; there's no official library for collecting public data, and its Research API is limited to approved academics.

Can you run the script on a schedule?

Yes, with cron or an Apify schedule; see how to monitor TikTok trends with an API.

Can you use JavaScript instead?

Yes, the apify-client npm package works the same way.

Where do the results go?

Into each run's dataset on Apify, which you can also download as JSON, CSV or Excel.