Skip to main content

A package for collecting TikTok videos and metadata.

Project description

Pyktok

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

By @dfreelon with contributions from @pkreissel, @p-bach, and @TimoBaeuerle

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 as well as from hidden APIs with no public documentation. Here are its major features, most of which require the URL(s) of the content you wish to collect:

  • Download TikTok videos
  • Download video metadata
  • Download up to 20 video comments
  • Download up to 30 most recent user video URLs
  • Download full TikTok JSON data objects (in case you want to extract data from parts of the object not included in the above functions)
  • Download TikTok metadata and video from search pages (thanks @p-bach and @TimoBaeuerle)

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.

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

Installation

pip install pyktok (still a version behind but will fix soon)

Requirements

Pyktok relies on the following packages:

Usage

import pyktok as pyk

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 get a list of URLs of up to 30 of a user's most recent videos (note, for this to work you must set the browser_name parameter to a browser on your system that you have used to access TikTok and that is supported by browser_cookie3. I think the only valid values are 'chrome' and 'firefox'):

tiktok_videos = pyk.get_account_video_urls('https://www.tiktok.com/@tiktok',browser_name='chrome')

To download metadata and comment data ONLY from video URLs collected via the preceding line 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.

pyk.save_tiktok_multi(tiktok_videos,
                      False,
                      'tiktok_data.csv',
                      1)

To get an individual video's JSON object:

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

To download all video comments initially visible on the page (previous versions of this function used TT's hidden API to capture many thousands of comments, but TT has disabled this feature, so the current function uses browser automation--which is unfortunately very slow--to pull only the comments that initially appear. However, you could use this function to download comments from videos periodically, which should give you a larger number of comments)

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

To download metadata for the keyword "funny" (practically speaking, you may not get all available data). This function has been rewritten since TT's data structure change, but should work. You can also download videos at the same time by setting save_videos to True.

pyk.save_tiktok_by_keyword('funny')

TikTok's servers may not love it if you run save_tiktok_multi, save_video_comments, or save_hashtag_video_urls 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyktok-0.0.11.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyktok-0.0.11-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file pyktok-0.0.11.tar.gz.

File metadata

  • Download URL: pyktok-0.0.11.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for pyktok-0.0.11.tar.gz
Algorithm Hash digest
SHA256 64bb39e51f4d37912cc091ad7d5c09098efa18980bf7d32c63c122cb4d9b7de3
MD5 5a775f6ca14c458754c924efe4e8d3fb
BLAKE2b-256 08d7cb797b0a6a4cfda3e7efd144f8326ffa55cd021db60521be1266dcb21367

See more details on using hashes here.

File details

Details for the file pyktok-0.0.11-py3-none-any.whl.

File metadata

  • Download URL: pyktok-0.0.11-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.12

File hashes

Hashes for pyktok-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 88ec12c73b18f349259338531e076bf2edc74adbf084cbe5f1c5d19651167d84
MD5 d4869b4656822dadc9d6d945f1c2bb72
BLAKE2b-256 d703a39ad027632c8a1607c23a42e4703af8e0e700c248322fb321d9652e1eae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page