Package: pyktok
PyktokA 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 videosDownload video metadataDownload 15-30 videos and/or metadata lines from hashtag, user, and music pages displaying multiple videosDownload full TikTok JSON data objects (in case you want to extract data from parts of the object not included in the above functions)R users, check outtraktok, an R port of Pyktok.This program may stop working suddenly if TikTok changes how it stores its data (seeFreelon, 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 thata 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.Installationpip install pyktokRequirementsPyktok relies on the following packages:BeautifulSoupbrowser-cookie3NumpyPandasRequestsSeleniumStreamlitUsageimportpyktokaspykpyk.specify_browser('chrome')We strongly recommend you run thespecify_browserfunction first to initialize Pyktok with a cookie from your browser of choice (e.g. 'firefox,' 'edge' etc.). If you don't, you must use thebrowser_nameparameter in most other functions, otherwise they will throw an error.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" ('chrome'can be changed to'firefox'if necessary):pyk.save_tiktok('https://www.tiktok.com/@tiktok/video/7106594312292453675?is_copy_url=1&is_from_webapp=v1',True,'video_data.csv','chrome')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','chrome')To download metadata ONLY from the video URLs used in the preceding two lines of code (to also download the videos, changeFalsetoTrue). 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 up to 30 metadata lines from auserpage (note, for this to work you must set thebrowser_nameparameter to a browser on your system that you have used to access TikTokandthat is supported bybrowser_cookie3. I think the only valid values are'chrome'and'firefox'. Also you can get the videos by settingsave_videotoTrue):pyk.save_tiktok_multi_page('https://www.tiktok.com/@tiktok',save_video=False,save_metadata=True,browser_name='chrome')To download up to 15 metadata lines from ahashtagpage:pyk.save_tiktok_multi_page('https://www.tiktok.com/tag/datascience?lang=en',save_video=False,save_metadata=True)To download up to 15 metadata lines from amusicpage:pyk.save_tiktok_multi_page('https://www.tiktok.com/music/Anti-Hero-7156822419213125634?lang=en',save_video=False,save_metadata=True)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')TikTok's servers may not love it if you run some of the above functions at full speed, so I recommend increasing thesleepparameter 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 usingstreamlit. To do so, simply navigate to yourpyktokdirectory in a command prompt (it should contain the fileapp.py) and runstreamlit run app.py. This should pop up a browser window that allows you to control Pyktok using graphical affordances.Mostly by@dfreelonwith contributions from (in chronological order):@pkreissel@p-bach@TimoBaeuerle@christinapwalker@codeteme@dphiffer@BillyBSig