How To Scrape TikTok Likes Using Python! (No API)

Welcome back! As you know TikTok is an insanely popular social media platform right now, so let’s scrape some data from it! In this use case, we’re going to be scraping the amount of likes from a single TikTok video. In later articles i’ll show you how to scrape entire videos in someones profile for likes, comments, shares, even the songs they use, but we have to crawl before we can walk, so let’s get started!

Basic introduction you could probably skip

First things first, we will need to have Python installed, read my article here to make sure you have Python and some IDE installed. Next, I wrote a article on using Selenium in Python, Selenium is a web scraping package that allows us to mimic a web browser using Python, it might be best to read that article for more of an understanding on web scraping, but it’s not a necessity, you can read that article here.

Let’s Get Started!

Now that we have the environment setup, we want to open up a blank python script. We then want to install the Selenium package using “pip install selenium” or “pip3 install selenium” or any other method you install Python packages in your IDE. Next, we want to go ahead and import the following modules into our Python script:

Now as I stated in my other article, we will be using Chrome as our browser, but Selenium can be used as a Firefox or Safari browser as well. The last line is an optional piece of code, I needed it to run my script, you may or may not need it.

Now that we have our packages loaded up, we want to declare our browser. As stated before, Selenium mimics a web browser, so it’ll literally open up a web browser on our page when we set a website to the driver, we can turn this off eventually but in this case we’ll keep it on. The following code is exactly how you initialize the chrome web browser using Selenium:

Now that we have our web browser initialized, all we have to do now is send our web browser to the tiktok video we want scrape from, in Selenium we use the “.get” function like so:

the text in the green is where our TikTok video will go, so now let’s make our way over to TikTok. Once we’re on the TikTok website, all we need to do is find a video we want to get the likes from, in this case I used Gordan Ramsays TikTok since his was the first one to show up. So let’s go into his profile and click on a random video, here is where we are at now:

At this point, we want to enable the developer options on our web browser, just Google “how to turn on developer options on insert your web browser here”. Once you have the developer options enabled, let’s go ahead and right click on the text under the heart which shows how many likes we have, then we can click on inspect element and a block of code will show up to the side of the screen just like this:

At this point, we want to find the specific line that matches up with the number of likes on the post, once that’s set where almost done! Go ahead and copy the URL of this video link and paste it into the “driver.get” code we had earlier so it looks something like this:

Once that’s done, we have to use the xpath of the likes element from the website as the placeholder to scrape the number of likes from, luckily Selenium makes it easy for us with the “find_element_by_xpath” function. I was going to write out a paragraph explaining about xpath’s but it’s not worth it, just look at the code and copy it over:

TEXT = driver.find_element_by_xpath('/html/body/div/div/div[2]/div[2]/div/div/main/div/div[1]/span[1]/div/div[1]/div[4]/div[2]/div[1]/strong').text

So once we have this code copied over, the text in the green is what’s going to be changed (keep the quotes). So let’s make our way back to the TikTok video, once there we want to click on the highlight code block on the right, and right click > Copy > Copy full xpath OR Copy xpath:

Once copied, paste it in between the quotes of the driver.find function as mentioned above, but make sure to delete the green code before hand. Once done, we want a line of code that will print out the number of likes, so add a print(text) line at the end just like this:

So at this point, the finished code will look like this:

#IMPORT THESE PACKAGES
import selenium
from selenium import webdriver
#OPTIONAL PACKAGE, BUY MAYBE NEEDED
from webdriver_manager.chrome import ChromeDriverManager

#THIS INITIALIZES THE DRIVER (AKA THE WEB BROWSER)
driver = webdriver.Chrome(ChromeDriverManager().install())

#THIS PRETTY MUCH TELLS THE WEB BROWSER WHICH WEBSITE TO GO TO
driver.get('https://www.tiktok.com/@gordonramsayofficial/video/6916583398500748550?lang=en')

#THIS IS THE IMPORTANT PART SO I'LL BREAK IT DOWN IN A COUPLE DIFFERENT PARTS LOL

#THIS 'TEXT' PORTION | THIS PORTION WILL TAKE THE ELEMENT THAT
#PRETTY MUCH STORES THE | WE WANT FROM THE WEBSITE, THE .TEXT WILL
#WEBSITE DATA THAT WE WANT | SAVE THE INFORMATION AS A TEXT DOCUMENT
#IN THIS VARIABLE |
TEXT = driver.find_element_by_xpath('/html/body/div/div/div[2]/div[2]/div/div/main/div/div[1]/span[1]/div/div[1]/div[4]/div[2]/div[1]/strong').text

print(TEXT)

Let’s go ahead and run it, you’ll see a web browser open, then go to the tiktok webpage, then it’ll go ahead and print out the number of likes in the Python console:

Congratulations! You just programmatically got the number of likes from a single TikTok video! Have fun TikTok scraping!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store