Skip to main content

Pyktok

A simple module to collect video, text, and metadata from TikTok.

We developed Pyktok ("pick-tock") because none of the existing TikTok data collection utilities we could find suited our needs. Pyktok pulls its data directly from the JSON objects embedded in Tiktok pages and from hidden APIs with no public documentation. Here are its major features:

  • Download TikTok videos
  • Download video metadata
  • Download around 30 videos and/or metadata lines from hashtag, user, and the "You May Like" sections of video pages (you can try to specify the exact number but TikTok doesn't always follow it exactly)
  • Download video comments
  • Download full TikTok JSON data objects (in case you want to extract data from parts of the object not included in the above functions)

Like Pyktok? Want to say thanks (and help fund future updates)? I accept Venmo and Cash App.

R users, check out traktok, an R port of Pyktok.

This program may stop working suddenly if TikTok changes how it stores its data (see Freelon, 2018). Some functions may require you to be logged in to TikTok, which means they may not work on cloud servers that don't have Chrome or Firefox installed.

TikTok now offers an official research API to US-based academics--you can apply for access here.

Please note that a number of US states have banned TikTok on state-owned devices, which may include IT resources provided by state universities. Individuals employed by such institutions are advised to use Pyktok only within applicable laws and regulations.

Installation

  1. pip install pyktok
  2. You'll also need to install the binaries for playwright using the playwright install (and possibly playwright install-deps) command in your local console.

Requirements

Pyktok relies on the following external packages:

Usage

import pyktok as pyk
pyk.specify_browser('chrome') #browser specification may or may not be necessary depending on your local settings

Historically it has been prudent to run the specify_browser function first to initialize Pyktok with a cookie from your browser of choice (e.g. 'firefox,' 'edge' etc.). However, recent developments indicate this may not be necessary for all users. YMMV.

Please note that functions that start with "get" will return data to working memory, while those starting with "save" will save data to disk without returning anything.

To download a single TikTok video and one line of metadata to the file "video_data.csv":

pyk.save_tiktok('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1',
	        True,
                'video_data.csv')

To download another TikTok video and add its metadata to the same file as above:

pyk.save_tiktok('https://www.tiktok.com/@tiktok/video/7011536772089924869?is_copy_url=1&is_from_webapp=v1',
	        True,
                'video_data.csv')

To download metadata ONLY from the video URLs used in the preceding two lines of code (to also download the videos, change False to True). If TikTok autobans the scraper, try changing the 1 to a higher number to increase the number of seconds between executions.

tiktok_videos = ['https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1',
                 'https://www.tiktok.com/@tiktok/video/7011536772089924869?is_copy_url=1&is_from_webapp=v1']
pyk.save_tiktok_multi_urls(tiktok_videos,
                           False,
                     	  'tiktok_data.csv',
                     	   1)

To download around 30 metadata lines from a user page (you can get the videos by setting save_video to True):

pyk.save_tiktok_multi_page('tiktok',ent_type='user',save_video=False,metadata_fn='tiktok.csv')

If this or the following two functions delivers an EmptyResponseException, try setting headless=False.

To download around 30 metadata lines from a hashtag page:

pyk.save_tiktok_multi_page('datascience',ent_type='hashtag',save_video=False,metadata_fn='datascience.csv')

To download around 30 metadata lines from related videos from a video page:

pyk.save_tiktok_multi_page('https://www.tiktok.com/@tiktok/video/7106594312292453675',ent_type='video_related',save_video=False,metadata_fn='7106594312292453675.csv')

To download around 30 comments from a video (the underlying code interprets comment_count rather loosely):

pyk.save_tiktok_comments('https://www.tiktok.com/@tiktok/video/7106594312292453675',comment_count=30,save_comments=True,return_comments=False)

To get an individual video's JSON object:

tt_json = pyk.alt_get_tiktok_json('https://www.tiktok.com/@tiktok/video/7011536772089924869?is_copy_url=1&is_from_webapp=v1')

TikTok's servers may not love it if you run some of the above functions at full speed, so I recommend increasing the sleep parameter if you get autobanned. I haven't tested this extensively so I have no idea if or when autobans start to kick in.

Pyktok can also be run from a browser window using streamlit. To do so, simply navigate to your pyktok directory in a command prompt (it should contain the file app.py) and run streamlit run app.py. This should pop up a browser window that allows you to control Pyktok using graphical affordances.

Mostly written by @dfreelon with contributions from (in chronological order):

Metadata

Release files for pyktok 0.0.31

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyktok 0.0.31
File Size Uploaded
pyktok-0.0.31.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyktok 0.0.31
File Interpreter ABI Platform
pyktok-0.0.31-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / pyktok-0.0.31.tar.gz

Download URL pyktok-0.0.31.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fb358f5cae264067d06fcf5c4975512a2b0f032720b22bab3018ef4df9fde47c
BLAKE2b-256 checksum
How to use checksums
34f3aa83891d66ca7942459e74bc2cb6a32d725e9d3ec537bdafbcc25b551eea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release files / pyktok-0.0.31-py3-none-any.whl

Download URL pyktok-0.0.31-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1c74b2be583896d7aa7468b243b27fdbdd6ffc8c1334f2e46f0cec930de7a679
BLAKE2b-256 checksum
How to use checksums
4962ee51490e8c63eb0773736f3dbf42b6a7721b0d537f0bb76c86a71223cc35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

0.0.31 This release

2 release files

0.0.30

2 release files

0.0.29

2 release files

0.0.27

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.18

2 release files

0.0.16

2 release files

0.0.14

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page