How to extract data from TikTok in three lines with Python

02 February 2023
Andrea Ramazzina
Chief Scientific 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 to name a few.

What are EnsembleData’s APIs?

Basically, what we offer is a set of APIs to extract data from the major social media 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 a direct and efficient way to extract data across different social media at scale.

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

EnsembleData dashboard
Preview of the dashboard. Show top left is the API token used to authenticate requests to the EnsembleData API

Reminder: You first need to activate your API token by verifying your email address, via the link sent to you upon signing up!

Using the Python library to make an request

Once you’ve registered and got your API token handy, you can start using the EnsembleData API! One method is to directly call the API directly via HTTP requests; in the documentation there are examples on how you can do this.

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 for a certain hashtag, for example, to monitor its usage or to spot some new trends.

First you need to setup a virtual env and install the ensembledata python package:

python3 -m venv .venv
source .venv/bin/activate

pip install ensembledata

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

from ensembledata.api import EDClient

client = EDClient("INSERT API TOKEN HERE")

We are now ready to send the request! Now we’ll call the method, passing as input the hashtag (in this case ‘magic’) and any other (required) parameters needed, in this case, ‘cursor’:

result = client.tiktok.hashtag_search(hashtag="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:", result.data["nextCursor"])
result = client.tiktok.hashtag_search(hashtag="magic", cursor=result.data["nextCursor"])

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’re depleted, you need to wait until they are reset (at midnight UTC). You can check how many you have used either on the dashboard or via one of our API endpoints.

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.