Stay tuned! Big things to come.

How to extract data from TikTok in three lines with Python

02 February 2023
Andrea Ramazzina
Chief Scientist Officer

If you are working in marketing, public relations or as a content creator, you’ve certainly discovered how crucial it is to have an data-driven understanding of what is going on across the major social media. Together with other long-standing platforms such as Twitter and Instagram, TikTok has risen to become one of the most popular social media out there.

However, extracting and processing large volumes of data in a reliable and consistent way from TikTok is no simple task!

In this blog post, we show how we can use EnsembleData Tiktok API to extract data from TikTok in 3 lines of code and how these simple APIs can unlock infinite use-cases such as brand monitoring or influencer analysis.

What are EnsembleData’s APIs?

Basically, what we offer is a set of APIs to extract data from all the major social medias, such as TikTok, Instagram and Youtube. All you need to do is make an API call and they do all the hard work, scraping and retrieving the requested data for you. In other words, it is an direct and efficient way to extract data across different social media at scale.

You can register for free on EnsembleData’s platform, to start using their APIs for free. In order to use the APIs, you just need your personal access token. You can find it once registered in the column on the left, as highlighted in red below:

EnsembleData dashboard
Example image of the dashboard. Highlighted personal API token used to validate the requests to the EnsembleData APIs

Remember that in order for the token to be active, you need to validate your email address!

Using the Python library to make an request

Once registered and got the token, you can start using the APIs! One method is to directly send the requests to; in the documentation there are examples on how to perform the calls. Alternatively, you can use the Python library. This provides a simpler and easier interface to send the requests.

Let’s take a look now at a very simple example. Suppose you want to get recent posts using a certain hashtag, for example, to monitor its usage or to spot some new trends.

First you would need to download and access the library locally:

git clone https://github.com/EnsembleData/TikTok-Scraper
cd TikTok-Scraper

The main component is the class Tiktok_I_ED. This allows you to perform all the different calls from one interface. When creating an instance of it, we pass the auth token (which we got in the previous step).

from tiktok_interface import Tiktok_I_ED
TOKEN = "INSERT YOUR TOKEN HERE"
tt = Tiktok_I_ED(token_ED_API=TOKEN)

We are now ready to perform the call! Now we’ll call the method, passing as input the search keyword name (in this case magic) and any other (required) parameters needed, in this case cursor:

res, success = tt.get_hashtag_posts(name = "magic", cursor = 0)

As simple as that! We just got all the data available, for a batch of 20 posts using that keyword. Say we want to continue with the search of other posts. In this case, we just need to call again the API, simply increasing the cursor:

print("Next cursor (needed to fetch the next batch):",res["nextCursor"])
res, success = tt.get_hashtag_posts(name = "magic", cursor = res["nextCursor"])

In this way you can continue extracting as much data as you want, using a simple one-liner.

Remember to check how many units you have used and how many you have left. Once they are over, you need to wait for the reset at midnight UTC (or of course upgrade the plan 🤗). You can check how many you have used either on the dashboard or through an API call

In case you want the compact code of this example, you can find it in the Github repository.

In case you want to extract data not just from TikTok but from all the major social media, you can use the main ED library, which wraps all the different APIs together. All the functions present in the two repository are the same, so you can use them exchangeably.

And that is it for today!

In the next post of this series, we are going to see how we can leverage such data to perform some analysis on an user or influencer, extracting valuable insights which would be unattainable otherwise.