How to Scrape TikTok Comments
Tags: tiktok, webscraping, datascraping, python
Published: 2024-03-28 20:59:24

---
title: How to Scrape TikTok Comments
published: true
description: Learn how to scrape TikTok comments with our easy step-by-step tutorial. Use Crawlbase free requests to scrape TikTok comments for free.
tags: tiktok, webscraping, datascraping, python
cover_image: https://crawlbase.com/blog/how-to-scrape-tiktok-comments/how-to-scrape-tiktok-comments-og.jpg
canonical_url: https://crawlbase.com/blog/how-to-scrape-tiktok-comments/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2024-03-27 16:59:31 +0000
---


This blog was originally posted to [Crawlbase Blog](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution)

In this blog, we will learn how to scrape TikTok comments. If you want a detailed tutorial on [scraping TikTok](https://crawlbase.com/blog/how-to-scrape-tiktok/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Scraping TikTok') data, check out to our guide on ‘[How to Scrape TikTok](https://crawlbase.com/blog/how-to-scrape-tiktok/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape TikTok')’.

<!-- more -->

In 2024, TikTok has more than [4 billion downloads](https://sproutsocial.com/insights/tiktok-stats/ 'TikTok Downloads') worldwide, making it one of the fastest-growing social media platforms lately. People of all ages, from teens to young adults and even older folks, are using it, which adds to its huge popularity and influence on culture.

![TikTok age & gender wise statistics 'TikTok age & gender wise statistics'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/tiktok-age-gender-statistics.jpg)


Comments on TikTok videos provide insights into user engagement, sentiments, trends, and much more. TikTok comments data enable researchers, marketers, and data enthusiasts to delve into user interactions, identify trending content, and gain a better understanding of TikTok's vibrant community.

So let’s start scraping TikTok comments in [Python](https://www.python.org/ 'Python').

## Table of Contents

1. [**TikTok Comment Scraper Essentials**](#1-TikTok-Comment-Scraper-Essentials)
2. [**Setup the Python Environment and Install Necessary Libraries**](#2-Setup-the-Python-Environment-and-Install-Necessary-Libraries)
3. [**Extract TikTok Video Comments HTML**](#3-Extract-TikTok-Video-Comments-HTML)
4. [**Extract TikTok Comments in JSON format**](#4-Extract-TikTok-Comments-in-JSON-format)
5. [**Handle Pagination in TikTok comments Scraping**](#5-Handle-Pagination-in-TikTok-comments-Scraping)
6. [**Saving Scraped TikTok Comments Data**](#6-Saving-Scraped-TikTok-Comments-Data)
7. [**Complete Code with Pagination & Saving**](#7-Complete-Code-with-Pagination-Saving)
8. [**Frequently Asked Questions (FAQs)**](#8-Frequently-Asked-Questions-FAQs)

## 1. TikTok Comment Scraper Essentials

You need to have basic knowledge of Python language, HTML and CSS for this tutorial. Secondly, setup [Crawlbase Crawling API](https://crawlbase.com/crawling-api-avoid-captchas-blocks?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Crawlbase Crawling API') to scrape comments from TikTok. All you need to do is [signup](https://crawlbase.com/signup?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Signup') to Crawlbase and you’ll get free 1000 requests to scrape TikTok comments free.

Head towards [Account Documentation](https://crawlbase.com/dashboard/account/docs?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Account Documentation') to get your Token.

![Account Documentation 'Account Documentation'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/account-documentation.jpg)


In your dashboard you’ll find two types of tokens.The Normal Token is for regular websites, and the JS Token is for websites with a lot of dynamic or JavaScript content. Since TikTok uses a lot of JavaScript, we'll use the JS Token.

Now for the Python setup, make sure you have Python installed. You can download and install Python from the official [Python website](https://www.python.org/downloads/ 'Python Website') based on your operating system. Additionally, make sure you have pip, the Python package manager, installed to install necessary libraries and dependencies.

Now you're well-equipped to start scraping TikTok video comments using Python and the Crawlbase Crawling API.

## 2. Setup the Python Environment and Install Necessary Libraries

To begin scraping TikTok video comments, follow these steps to set up your project environment:

**Python Installation**: If you haven't already, download and install Python from the official Python website. Make sure to add Python to your system's PATH during installation. You can verify the installation by opening a command prompt or terminal and running the following command:

```bash
python --version
```

If Python is installed correctly, you'll see the installed version number.

**Create a New Python Environment**: It's recommended to work within a virtual environment to manage project dependencies. Create a new virtual environment using the following commands:

```bash
# Create a new virtual environment
python -m venv myenv

# Activate the virtual environment
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate
```

**Install Necessary Libraries**: Install the required Python libraries for web scraping and data extraction. Key libraries include `requests`, `beautifulsoup4`, and `crawlbase`. You can install these libraries using pip, the Python package manager:

```bash
pip install requests beautifulsoup4 crawlbase
```

**Initialize Project Files**: Create a new Python script file `tiktok_comments_scraper.py` for your TikTok comments scraping project. You can use any text editor or integrated development environment (IDE) of your choice to write your Python code.

With your project environment set up and libraries installed, you're ready to start scraping TikTok video comments. Let's move into the next step.

## 3. Extract TikTok Video Comments HTML

To begin scraping TikTok video comments, we need to retrieve the HTML content of the TikTok page where the comments are located. There are two common approaches to accomplish this: using a **standard HTTP request library** like `requests` or using the Crawlbase Crawling API..

### Extracting TikTok Comments HTML using Common Approach

In the common approach, we use Python libraries such as `requests` to fetch the HTML content of the TikTok video page. Here's a basic example of how you can achieve this:

```python
import requests

# Function to fetch HTML using requests
def fetch_html(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
        else:
            print(f"Failed to fetch HTML. Status code: {response.status_code}")
            return None
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None

# Example usage
tiktok_url = "https://www.tiktok.com/@khaby.lame/video/7255327059302419738"
html_content = fetch_html(tiktok_url)
if html_content:
    print(html_content)
```

Copy above code into your `tiktok_comments_scraper.py` file and run the following command in the directory where file is present.

```bash
python tiktok_comments_scraper.py
```

You will see that the HTML of the page get printed on the terminal.

![Common Approach HTML Output 'Common Approach HTML Output'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/common-approach-html-output.jpg)


**But why there is no useful information in the HTML?** It's because TikTok relies on JavaScript rendering to load essential data dynamically. Unfortunately, with conventional scraping methods, accessing this data can be challenging.

**Common Scraping Approaches Limitations**

The common approach of fetching HTML using libraries like `requests` may encounter limitations when scraping TikTok video comments. Some of the issues with this approach include:

![Common Web Scraping Approach Issues 'Common Web Scraping Approach Issues'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/common-web-scraping-approach-issues.jpg)


1. **Limited JavaScript Execution**: Standard HTTP requests do not execute JavaScript, which means that dynamically loaded content, such as comments on TikTok videos, may not be captured accurately.
2. **Incomplete Data Retrieval**: TikTok pages often load comments asynchronously or through AJAX requests, which may not be fully captured by a single HTTP request. As a result, the fetched HTML may lack certain elements or contain placeholders instead of actual comments.
3. **Rate Limiting and IP Blocking**: Continuous scraping using traditional methods may trigger rate limits or IP blocking mechanisms on the TikTok servers, leading to restricted access or temporary bans.

To overcome these issues and ensure accurate scraping of TikTok video comments, alternative methods such as using **headless browsers** or **dynamic rendering APIs** may be necessary. One of the API out there is [**Crawlbase Crawling API**](https://crawlbase.com/crawling-api-avoid-captchas-blocks?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Crawlbase Crawling API').

## Extracting HTML Using Crawlbase Crawling API

To overcome the limitations of the common approach and efficiently fetch TikTok video comments HTML, we can utilize the [Crawlbase Crawling API](https://crawlbase.com/crawling-api-avoid-captchas-blocks?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Crawlbase Crawling API'). This API allows us to fetch the rendered HTML content of TikTok pages, including **dynamically loaded content**.

Here's how you can use the Crawlbase Crawling API to retrieve TikTok video comments HTML:

```python
from crawlbase import CrawlingAPI

# Initialize the Crawlbase CrawlingAPI object
crawling_api = CrawlingAPI({"token": "CRAWLBASE_JS_TOKEN"})

options = {
    'ajax_wait': 'true',
    'page_wait': 10000,
    "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
}

# Function to fetch HTML using Crawlbase Crawling API
def fetch_html_crawlbase(url):
    global crawling_api, options
    try:
        response = crawling_api.get(url, options)
        if response['headers']['pc_status'] == '200':
            return response['body'].decode('utf-8')
        else:
            print(f"Failed to fetch HTML. Crawlbase status code: {response['headers']['pc_status']}")
            return None
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None

# Example usage
tiktok_url = "https://www.tiktok.com/@khaby.lame/video/7255327059302419738"
html_content = fetch_html_crawlbase(tiktok_url)
if html_content:
    print(html_content)
```

Using the Crawlbase Crawling API allows us to fetch the HTML content of TikTok pages efficiently, ensuring that JavaScript-rendered content is captured accurately. This approach is particularly useful for scraping dynamic content like TikTok video comments.

![Crawling API HTML Output 'Crawling API HTML Output'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/crawling-api-html-output.jpg)


## 4. Extract TikTok Comments in JSON format

Scraping TikTok comments involves extracting various components such as video author information, comments listing, comment content, and commenter details. Let's deeply diagnose each step along with professional and efficient code examples.

### Scraping Video Author Information

When scraping comments from the video page, it's crucial to maintain accurate records of both the corresponding video and its uploader for comprehensive analysis and attribution. To scrape the video author information from a TikTok video page, we can extract details such as the username, profile URL, and profile picture.

![Video Author HTML Elements 'Video Author HTML Elements'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/video-author-information.jpg)


Here's a code example demonstrating how to accomplish this:

```python
def scrape_video_author_info(soup):
    author_info = {}
    author_info["Username"] = soup.select_one("span[data-e2e='browse-username']").text.strip()
    author_info["User Profile URL"] = soup.select_one("a[data-e2e='browse-user-avatar']")["href"]
    author_info["User Picture URL"] = soup.select_one("a[data-e2e='browse-user-avatar'] img")["src"]
    return author_info
```

### Scraping Comments Listing

To scrape the comments listing from a TikTok video page, we can extract the HTML elements containing the comments.

![Comment Listing HTML Elements 'Comment Listing HTML Elements'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/video-comments-listing.jpg)


Here's a code example demonstrating how to accomplish this:

```python
def scrape_comments_listing(soup):
    comments_listing = soup.select("div[data-e2e='search-comment-container'] > div[class*='CommentListContainer'] > div[class*='DivCommentItemContainer']")
    return comments_listing
```

### Scraping Comment Content

To scrape the content of each comment, we can extract the text content of the comment elements.

![Comment Content HTML Element 'Comment Content HTML Element'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/video-comment-content.jpg)


Here's a code example demonstrating how to accomplish this:

```python
def scrape_comment_content(comment):
    comment_content = {}
    comment_content['Comment Content'] = comment.select_one("div[class*='DivCommentContentContainer'] p[data-e2e='comment-level-1'] > span").text.strip()
    return comment_content
```

### Scraping Commenter Details

To scrape details about the commenter, such as their username and profile URL, we can extract relevant information from the comment elements.

![Commenter details HTML Elements 'Commenter details HTML Elements'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/video-commenter-details.jpg)


Here's a code example demonstrating how to accomplish this:

```python
def scrape_commenter_details(comment):
    commenter_details = {}
    commenter_details["Commenter Username"] = comment.select_one("div[class*='DivCommentContentContainer'] span[data-e2e='comment-username-1']").text.strip()
    commenter_details["Commenter Profile URL"] = comment.select_one("div[class*='DivCommentContentContainer'] > a[data-e2e='comment-avatar-1']").get("href")
    return commenter_details
```

### Complete Code

Now, let's combine these scraping functions into a complete code example that extracts video author information, comments listing, comment content, and commenter details:

```python
from crawlbase import CrawlingAPI
from bs4 import BeautifulSoup
import json

# Initialize the Crawlbase CrawlingAPI object
crawling_api = CrawlingAPI({"token": "CRAWLBASE_JS_TOKEN"})

options = {
    'ajax_wait': 'true',
    'page_wait': 10000,
    "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
}

# Function to fetch HTML using Crawlbase Crawling API
def fetch_html_crawlbase(url):
    global crawling_api, options
    try:
        response = crawling_api.get(url, options)
        if response['headers']['pc_status'] == '200':
            return response['body'].decode('utf-8')
        else:
            print(f"Failed to fetch HTML. Crawlbase status code: {response['headers']['pc_status']}")
            return None
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None

def scrape_video_author_info(soup):
    author_info = {}
    author_info["Username"] = soup.select_one("span[data-e2e='browse-username']").text.strip()
    author_info["User Profile URL"] = soup.select_one("a[data-e2e='browse-user-avatar']")["href"]
    author_info["User Picture URL"] = soup.select_one("a[data-e2e='browse-user-avatar'] img")["src"]
    return author_info

def scrape_comments_listing(soup):
    comments_listing = soup.select("div[data-e2e='search-comment-container'] > div[class*='CommentListContainer'] > div[class*='DivCommentItemContainer']")
    return comments_listing

def scrape_comment_content(comment):
    comment_content = {}
    comment_content['Comment Content'] = comment.select_one("div[class*='DivCommentContentContainer'] p[data-e2e='comment-level-1'] > span").text.strip()
    return comment_content

def scrape_commenter_details(comment):
    commenter_details = {}
    commenter_details["Commenter Username"] = comment.select_one("div[class*='DivCommentContentContainer'] span[data-e2e='comment-username-1']").text.strip()
    commenter_details["Commenter Profile URL"] = comment.select_one("div[class*='DivCommentContentContainer'] > a[data-e2e='comment-avatar-1']").get("href")
    return commenter_details

def main():
    scraped_data = {}

    # Fetch HTML content of the TikTok video page
    html_content = fetch_html_crawlbase("https://www.tiktok.com/@khaby.lame/video/7255327059302419738")

    # Parse HTML content using BeautifulSoup
    soup = BeautifulSoup(html_content, "html.parser")

    # Scrape video author information
    scraped_data['Author Information'] = scrape_video_author_info(soup)

    # Scrape comments listing
    comments_listing = scrape_comments_listing(soup)

    # Iterate through comments and scrape comment content and commenter details
    comments_list = []
    for comment in comments_listing:
        comments_detail = {}
        comments_detail.update(scrape_comment_content(comment))
        comments_detail.update(scrape_commenter_details(comment))
        comments_list.append(comments_detail)

    scraped_data['Comments'] = comments_list

    # Print the scraped results
    print(json.dumps(scraped_data, indent=2, ensure_ascii=False))

if __name__ == "__main__":
    main()
```

Example Output:

```json
{
  "Author Information": {
    "Username": "khaby.lame",
    "User Profile URL": "https://www.tiktok.com/@khaby.lame",
    "User Picture URL": "./Always trust Robert Downey Jr #oppenheimer #imax _ TikTok_files/6595ee956e2304779514b56c2f9cb666~c5_100x100.jpeg"
  },
  "Comments": [
    {
      "Comment Content": "its that iron man ?",
      "Commenter Username": "dezzznuts",
      "Commenter Profile URL": "https://www.tiktok.com/@dezzznuts19"
    },
    {
      "Comment Content": "wait he's alive?",
      "Commenter Username": "Mr.Blurryface",
      "Commenter Profile URL": "https://www.tiktok.com/@mr.blurryfacee21"
    },
    {
      "Comment Content": "tony?😳",
      "Commenter Username": "franciscorosalles",
      "Commenter Profile URL": "https://www.tiktok.com/@franciscorosalles"
    },
    {
      "Comment Content": "is that tony",
      "Commenter Username": "DeepDeath",
      "Commenter Profile URL": "https://www.tiktok.com/@gt.clock.a"
    },
    {
      "Comment Content": "second",
      "Commenter Username": "🇷🇸☠️🇷🇸",
      "Commenter Profile URL": "https://www.tiktok.com/@tiktok_zenox7"
    },
    {
      "Comment Content": "the best 💗💗💗",
      "Commenter Username": "🍓  L e t i  🍓",
      "Commenter Profile URL": "https://www.tiktok.com/@leticiiamurilloo"
    },
    {
      "Comment Content": "iron man 😱",
      "Commenter Username": "Fishcopter",
      "Commenter Profile URL": "https://www.tiktok.com/@hugobosskingman.cool88"
    },
    ..... more
  ]
}
```

## 5. Handle Pagination in TikTok comments Scraping

TikTok often employs infinite scrolling to load more comments dynamically. To handle pagination, we can utilize the Crawlbase Crawling API [`scroll`](https://crawlbase.com/docs/crawling-api/parameters/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution#scroll 'Scroll') parameter. By default, the scroll interval is set to 10 seconds, but you can use `scroll_interval` parameter to change it. Here's an example of how to handle pagination using the Crawlbase Crawling API:

```python
# Function to fetch HTML content with scroll pagination
def fetch_html_with_pagination_crawlbase(url):
    global crawling_api

    # Set up Crawlbase API parameters for scroll pagination
    options = {
        'ajax_wait': 'true',
        'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0',
        'scroll': 'true',
        'scroll_interval': 20000  # (20 seconds)
    }

    try:
        response = crawling_api.get(url, options)
        if response['headers']['pc_status'] == '200':
            return response['body'].decode('utf-8')
        else:
            print(f"Failed to fetch HTML. Crawlbase status code: {response['headers']['pc_status']}")
            return None
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        return None
```

## 6. Saving Scraped TikTok Comments Data

After successfully scraping TikTok comments, it's crucial to save the extracted data for further analysis and future reference. Here, we'll explore two common methods for storing scraped TikTok comments data: saving to an Excel file and saving to an SQLite database.

### Saving to Excel File

Excel files provide a convenient way to organize and analyze scraped data, making them a popular choice for storing structured information like TikTok comments. Python offers libraries like `pandas` for efficiently handling dataframes and `openpyxl` for writing data to Excel files.

```python
import pandas as pd

# Function to save comment data to Excel
def save_to_excel(data, filename):
    author_info = data['Author Information']
    comments = data['Comments']

    # Flatten author information
    flat_author_info = {'Author_Username': author_info['Username'],
                        'Author_Profile_URL': author_info['User Profile URL'],
                        'Author_Picture_URL': author_info['User Picture URL']}

    # Flatten comments
    flat_comments = [{'Comment_Content': comment['Comment Content'],
                      'Commenter_Username': comment['Commenter Username'],
                      'Commenter_Profile_URL': comment['Commenter Profile URL']} for comment in comments]

    # Create DataFrames
    author_df = pd.DataFrame([flat_author_info])
    comments_df = pd.DataFrame(flat_comments)

    # Save to Excel
    with pd.ExcelWriter(filename) as writer:
        author_df.to_excel(writer, sheet_name='Author Information', index=False)
        comments_df.to_excel(writer, sheet_name='Comments', index=False)

    print("Data saved to Excel file successfully.")
```

**`save_to_excel(data, filename)`**:

This function saves comment data along with author information to an Excel file. It takes two parameters: data, a dictionary containing both author information and comments, and filename, the name of the Excel file to which the data will be saved.

Inside the function:

- Author information and comments are extracted from the `data` dictionary.
- Author information is flattened into a dictionary `flat_author_info`.
- Comments are flattened into a list of dictionaries `flat_comments`.
- Two DataFrames are created: one for author information (`author_df`) and one for comments (`comments_df`).
- The DataFrames are written to different sheets in the Excel file using `pd.ExcelWriter`.
- Finally, a success message is printed indicating that the data has been saved to the Excel file.

### Saving to SQLite Database

SQLite databases offer a lightweight and self-contained solution for storing structured data locally. Python provides the sqlite3 module for interacting with SQLite databases. We can create a table to store TikTok comments data and insert the scraped data into the table.

```python
import sqlite3

# Function to create table if not exists
def create_table_if_not_exists(db_filename, table_name):
    try:
        conn = sqlite3.connect(db_filename)
        # Create or append to the SQLite table
        create_table_sql = f'''CREATE TABLE IF NOT EXISTS {table_name} (
                                Author_Username TEXT,
                                Author_Profile_URL TEXT,
                                Author_Picture_URL TEXT,
                                Comment_Content TEXT,
                                Commenter_Username TEXT,
                                Commenter_Profile_URL TEXT
                              )'''
        conn.execute(create_table_sql)
        conn.commit()
    except sqlite3.Error as e:
        print(f"Error creating SQLite table: {e}")
    finally:
        conn.close()

# Function to save comment data to SQLite database
def save_to_sqlite(data, db_filename, table_name):
    author_info = data['Author Information']
    comments = data['Comments']
    try:
        conn = sqlite3.connect(db_filename)

        # Flatten author information
        flat_author_info = (author_info['Username'],
                            author_info['User Profile URL'],
                            author_info['User Picture URL'])

        # Insert author data into SQLite
        insert_author_sql = f'''INSERT INTO {table_name} (
                                Author_Username,
                                Author_Profile_URL,
                                Author_Picture_URL,
                                Comment_Content,
                                Commenter_Username,
                                Commenter_Profile_URL
                              ) VALUES (?, ?, ?, ?, ?, ?)'''

        for comment in comments:
            flat_comment = (flat_author_info[0],  # Author_Username
                            flat_author_info[1],  # Author_Profile_URL
                            flat_author_info[2],  # Author_Picture_URL
                            comment['Comment Content'],
                            comment['Commenter Username'],
                            comment['Commenter Profile URL'])

            conn.execute(insert_author_sql, flat_comment)

        conn.commit()
        print("Data saved to SQLite database successfully.")
    except sqlite3.Error as e:
        print(f"Error saving data to SQLite database: {e}")
    finally:
        conn.close()
```

**`create_table_if_not_exists(db_filename, table_name)`**:

This function ensures that a table exists in the SQLite database with the specified name. It takes two parameters: `db_filename`, the filename of the SQLite database, and `table_name`, the name of the table to be created or checked for existence.

Inside the function:

- A connection is established to the SQLite database.
- A SQL query is executed to create the table if it does not already exist. The table consists of fields for author information (username, profile URL, and picture URL) and comment information (content, commenter username, and commenter profile URL).
- If an error occurs during table creation, an error message is printed.
- Finally, the connection to the database is closed.

**`save_to_sqlite(data, db_filename, table_name)`**:

This function saves comment data along with author information to a SQLite database. It takes three parameters: `data`, a dictionary containing both author information and comments, `db_filename`, the filename of the SQLite database, and `table_name`, the name of the table to which the data will be saved.

Inside the function:

- Author information and comments are extracted from the `data` dictionary.
- Author information is flattened into a tuple `flat_author_info`.
- For each comment, the author information and comment data are combined into a tuple `flat_comment`.
- An SQL query is executed to insert the data into the SQLite table.
- If an error occurs during data insertion, an error message is printed.
- Finally, the connection to the database is closed.

## 7. Complete Code with Pagination & Saving

Now, let's extend our section 4 complete code example to include handling pagination and saving comment data to Excel and SQLite databases:

```python
from crawlbase import CrawlingAPI
from bs4 import BeautifulSoup
import pandas as pd
import sqlite3

# Initialize the Crawlbase CrawlingAPI object
crawling_api = CrawlingAPI({"token": "CRAWLBASE_JS_TOKEN"})

def fetch_html_with_pagination_crawlbase(url):
    # ... (unchanged)

def scrape_video_author_info(soup):
    # ... (unchanged)

def scrape_comments_listing(soup):
    # ... (unchanged)

def scrape_comment_content(comment):
    # ... (unchanged)

def scrape_commenter_details(comment):
    # ... (unchanged)

def save_to_excel(data, filename):
    # ... (unchanged)

def create_table_if_not_exists(db_filename, table_name):
    # ... (unchanged)

def save_to_sqlite(data, db_filename, table_name):
    # ... (unchanged)

def main():
    scraped_data = {}

    # Fetch HTML content of the TikTok video page
    html_content = fetch_html_with_pagination_crawlbase("https://www.tiktok.com/@khaby.lame/video/7255327059302419738")

    # Parse HTML content using BeautifulSoup
    soup = BeautifulSoup(html_content, "html.parser")

    # Scrape video author information
    scraped_data['Author Information'] = scrape_video_author_info(soup)

    # Scrape comments listing
    comments_listing = scrape_comments_listing(soup)

    # Iterate through comments and scrape comment content and commenter details
    comments_list = []
    for comment in comments_listing:
        comments_detail = {}
        comments_detail.update(scrape_comment_content(comment))
        comments_detail.update(scrape_commenter_details(comment))
        comments_list.append(comments_detail)

    scraped_data['Comments'] = comments_list

    # Save comment data to Excel and SQLite database
    save_to_excel(scraped_data, "tiktok_comments.xlsx")
    create_table_if_not_exists("tiktok_comments.db", "comments")
    save_to_sqlite(scraped_data, "tiktok_comments.db", "comments")

if __name__ == "__main__":
    main()
```

This extended code example demonstrates how to handle pagination while scraping TikTok comments and save the scraped comment data to both Excel and SQLite databases for further analysis.

`tiktok_comments.xlsx` File & `comments` table snapshot:

![Output Files Snapshot 'Output Files Snapshot'](https://crawlbase.com/blog/how-to-scrape-tiktok-comments/output-files-snapshot.jpg)


Congratulations on successfully setting up your TikTok comment scraper using the Crawlbase Crawling API and Python! I hope this guide was useful in scraping TikTok comments.

Do explore our curated list of [TikTok scrapers](https://crawlbase.com/blog/best-tiktok-scrapers-to-collect-data/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'TikTok Scrapers') to enhance your scraping capabilities:

📜 [Best TikTok Scrapers](https://crawlbase.com/blog/best-tiktok-scrapers-to-collect-data/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Best TikTok Scrapers')
📜 [How to Scrape TikTok Search Results](https://crawlbase.com/blog/how-to-scrape-tiktok/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape TikTok Search Results')
📜 [How to Scrape Facebook](https://crawlbase.com/blog/mastering-facebook-data-extraction-using-crawling-api/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape Facebook')
📜 [How to Scrape Linkedin](https://crawlbase.com/blog/how-to-crawl-linkedin-ruby/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape Linkedin')
📜 [How to Scrape Twitter](https://crawlbase.com/blog/twitter-profile-scraping-for-influence-analysis/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape Twitter')
📜 [How to Scrape Instagram](https://crawlbase.com/blog/how-to-scrape-instagram-data-using-python/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape Instagram')
📜 [How to Scrape Youtube](https://crawlbase.com/blog/youtube-channel-scraper/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'How to Scrape Youtube')

For further **customization options and advanced features**, refer to the [Crawlbase Crawling API](https://crawlbase.com/docs/crawling-api/?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Crawlbase Crawling API') documentation. If you have any questions or feedback, our [support team](https://crawlbase.com/dashboard/support?utm_source=dev.to&utm_medium=referral&utm_campaign=content_distribution 'Support Team') is always available to assist you on your web scraping journey.

## 8. Frequently Asked Questions (FAQs)

### Q. Why Scrape TikTok Comments?

Scraping TikTok comments allows users to extract valuable insights, trends, and sentiments from user-generated content. By analyzing comments, businesses can gain a better understanding of their audience's preferences, opinions, and feedback. Researchers can also use comment data for social studies, sentiment analysis, and trend analysis.

### Q. Is It Legal to Scrape TikTok Comments?

While scraping TikTok comments is technically against TikTok's terms of service, the legality of web scraping depends on various factors, including the purpose of scraping, compliance with data protection laws, and respect for website terms of service. It's essential to review and adhere to TikTok's terms of service and data protection regulations before scraping comments from the platform.

### Q. What Can You Learn from Scraping TikTok Comments?

Scraping TikTok comments can provide valuable insights into user engagement, sentiment analysis, content trends, and audience demographics. By analyzing comment data, users can identify popular topics, assess audience reactions to specific content, and understand user sentiment towards brands, products, or services.

### Q. How to Handle Dynamic Content While Scraping TikTok Comments?

Handling dynamic content while scraping TikTok comments involves using techniques such as using headless browsers or automation tools to render the page fully before extracting the comments. Alternatively, users can utilize web scraping APIs like Crawlbase Crawling API, which provide JavaScript rendering capabilities to scrape dynamic content accurately. These methods ensure that all comments, including dynamically loaded ones, are captured effectively during the scraping process.