DEV Community

Cover image for How to Fetch TikTok Data and Estimate Earnings Using Python
Max Widgets
Max Widgets

Posted on

How to Fetch TikTok Data and Estimate Earnings Using Python

TikTok doesn’t provide a fully open and flexible API for developers, which makes it difficult to analyze creator data or build tools around it.

In this post, we’ll:

  • Fetch TikTok data using an unofficial API
  • Extract useful metrics
  • Calculate engagement rate
  • Estimate earnings using a simple model

Step 1: Install Dependencies

pip install TikTokApi
python -m playwright install
Enter fullscreen mode Exit fullscreen mode

Step 2: Setup TikTok API Instance

You need a verifyFp value.

Get it from:

  • Open TikTok in browser
  • DevTools → Application → Cookies
  • Copy s_v_web_id value
from TikTokApi import TikTokApi

verifyFp = "your_verifyFp_here"

api = TikTokApi.get_instance(
    custom_verifyFp=verifyFp,
    use_test_endpoints=True
)
Enter fullscreen mode Exit fullscreen mode

Step 3: Fetch User Data

username = "khaby.lame"

user = api.get_user(username)

print(user)
Enter fullscreen mode Exit fullscreen mode

Step 4: Get User Stats

stats = user["stats"]

followers = stats["followerCount"]
likes = stats["heartCount"]
videos = stats["videoCount"]

print("Followers:", followers)
print("Total Likes:", likes)
print("Total Videos:", videos)
Enter fullscreen mode Exit fullscreen mode

Step 5: Fetch User Videos

videos = api.user_posts(username, count=10)

video_data = []

for video in videos:
    stats = video["stats"]

    views = stats["playCount"]
    likes = stats["diggCount"]
    comments = stats["commentCount"]
    shares = stats["shareCount"]

    video_data.append({
        "views": views,
        "likes": likes,
        "comments": comments,
        "shares": shares
    })

print(video_data)
Enter fullscreen mode Exit fullscreen mode

Step 6: Calculate Engagement Rate

def calculate_engagement(video_data):
    total_views = sum(v["views"] for v in video_data)
    total_engagement = sum(
        v["likes"] + v["comments"] + v["shares"] 
        for v in video_data
    )

    if total_views == 0:
        return 0

    return (total_engagement / total_views) * 100


engagement_rate = calculate_engagement(video_data)

print("Engagement Rate:", round(engagement_rate, 2), "%")
Enter fullscreen mode Exit fullscreen mode

Step 7: Estimate Earnings

Simple model:

  • Low engagement → $0.02 per 1000 views
  • Medium → $0.04
  • High → $0.08
def estimate_earnings(avg_views, engagement_rate):
    if engagement_rate < 3:
        cpm = 0.02
    elif engagement_rate < 6:
        cpm = 0.04
    else:
        cpm = 0.08

    earnings = (avg_views / 1000) * cpm
    return earnings


avg_views = sum(v["views"] for v in video_data) / len(video_data)

estimated_earnings = estimate_earnings(avg_views, engagement_rate)

print("Estimated Earnings per post: $", round(estimated_earnings, 2))
Enter fullscreen mode Exit fullscreen mode

Step 8: Get Video URLs

for video in videos:
    print(video["video"]["playAddr"])
Enter fullscreen mode Exit fullscreen mode

Final Thoughts

Using an unofficial API, we can:

  • Analyze TikTok creator performance
  • Build engagement tools
  • Estimate earnings

This is exactly how tools like a TikTok Money Calculator work behind the scenes.

If you don’t want to code everything from scratch, you can try a ready-made version here:

👉 https://ttmoneycalculator.com

Top comments (0)