A Python scraper for pulling public TikTok video data using the unofficial TikTokApi library. Supports scraping by hashtag, username, trending feed, or search keyword.
- Scrape videos by hashtag, user, trending, or search
- Extracts: views, likes, comments, shares, caption, hashtags, author info, video URL, music
- Outputs clean JSON files with auto-generated filenames
- Supports
msTokenfor authenticated sessions (reduces bot detection)
Requirements: Python 3.9+
pip install TikTokApi
playwright install chromium# By hashtag
python tiktok_scraper.py --mode hashtag --query cooking --count 50
# By user
python tiktok_scraper.py --mode user --query charlidamelio --count 30
# Trending feed
python tiktok_scraper.py --mode trending --count 20
# Search
python tiktok_scraper.py --mode search --query "pasta recipe" --count 40Output is saved to ./output/tiktok_<mode>_<query>_<timestamp>.json.
TikTok may block requests without a session token. To get one:
- Open tiktok.com in your browser and scroll around
- Open DevTools → Application → Cookies →
www.tiktok.com - Copy the value of the cookie named
msToken - Pass it via flag or environment variable:
# via flag
python tiktok_scraper.py --mode user --query yourname --ms-token YOUR_TOKEN
# via env var
export TIKTOK_MS_TOKEN=YOUR_TOKEN
python tiktok_scraper.py --mode user --query yournameEach video in the JSON includes:
| Field | Description |
|---|---|
id |
TikTok video ID |
created_at |
Upload timestamp (UTC) |
description |
Caption text |
hashtags |
List of hashtag names |
views |
Total play count |
likes |
Like count |
comments |
Comment count |
shares |
Share count |
author_username |
TikTok handle |
author_nickname |
Display name |
author_followers |
Follower count |
author_verified |
Verified status |
video_url |
Direct download URL (expires) |
cover_url |
Thumbnail URL |
duration_sec |
Video length in seconds |
music_title |
Audio track name |
- This uses the unofficial TikTokApi — TikTok occasionally changes their internals and breaks it. Run
pip install --upgrade TikTokApiif things stop working. video_urldownload links expire after a few hours.- Scraping TikTok without permission may violate their ToS. Use responsibly.
MIT