Links
Comment on page

Get user stats

Tags: #tiktok #user #stats #snippet #content
Author: Alok Chilka
Last update: 2023-04-12 (Created: 2022-04-12)
Description: This notebook provides an analysis of user statistics on the popular social media platform, TikTok.

Input

Import libraries

try:
from TikTokAPI import TikTokAPI
except:
!pip install --user PyTikTokAPI
from TikTokAPI import TikTokAPI
import nest_asyncio
import pandas as pd
nest_asyncio.apply()

Setup your TikTok

How to get cookies ?

  • "s_v_web_id and "tt_webid"
While logged into your tiktok account, Click F12 to open developer console in your browser. Navigate to path Storage -> Cookies -> https://www.tiktok.com and on right hand side there are the parameters marked which you will need.
image.png
# Cookies
cookie = {
"s_v_web_id": "verify_l0ecjehXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"tt_webid": "1%7CaSy-x8YGNmB_l9qsXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
}
# Username
username = "tiktester04"

Create connection object

api = TikTokAPI(cookie=cookie)

Model

Get user stats

def get_user_stats(username):
# Get user details
"""
User detail fields : video count, follower count, following count, heart count
"""
user_obj = api.getUserByName(username)
user_video_count = user_obj["userInfo"]["stats"]["videoCount"]
user_follower_count = user_obj["userInfo"]["stats"]["followerCount"]
user_following_count = user_obj["userInfo"]["stats"]["followingCount"]
user_heart_count = user_obj["userInfo"]["stats"]["heartCount"]
user_stats = {
"user_video_count": user_video_count,
"user_follower_count": user_follower_count,
"user_following_count": user_following_count,
"user_heart_count": user_heart_count,
}
return user_stats

Output

Get user stats

df_stats = get_user_stats(username)
df_stats