Fetching Post Comments¶
Post Comments API¶
To fetch comments we'll first need to find the post id, also known as the aweme_id
.
Where can I find the aweme_id
?
You can find the aweme_id
in the URL of the post. For example, in the URL https://www.tiktok.com/@username/video/6849400000000
the aweme_id
is 6849400000000
. Otherwise, you can also find the aweme_id
in the response for any of the endpoints which return post data.
Let's fetch some comments!
import requests
result = requests.get(
"https://ensembledata.com/apis/tt/post/comments",
params={
"aweme_id": "7411198650782731563",
"token": "API_TOKEN",
}
).json()["data"]
comments = result["comments"]
next_cursor = result.get("nextCursor", None)
print("Comments fetched:", len(comments))
print("Total comments:", result["total"])
import { EDClient } from "ensembledata";
const client = new EDClient({ token: "API_TOKEN" })
let result = await client.tiktok.postComments({
awemeId: "6849400000000"
})
let comments = result.data["comments"]
let nextCursor = result.data["nextCursor"]
console.log("Comments fetched:", comments.length)
console.log("Total comments:", result.data.total)
Output:
Sweet! Let's see what data we retrieved.
Comment data¶
Inside each comment object you'll find everything you need to know about the comment, including a variety of data about the commenter.
comment = comments[0]
user = comment["user"]
print("=== First comment ===")
print("comment_id:", comment["cid"]) # We'll need this for fetching replies!
print("timestamp:", comment["create_time"])
print("text:", comment["text"])
print("likes:", comment["digg_count"])
print("replies:", comment["reply_comment_count"])
print("username:", user["unique_id"])
Tip
If you need to get the more details about the commenter, the username
, uid
and sec_uid
fields will be useful for calling other API endpoints such as User Info From Secuid
, User Posts From Username
or User Followers
.
Fetching more comments¶
As you can see the Post Comments
API gives us up to 30 comments per request. Using the nextCursor
we got from the previous response, we can get the next batch of comments.
You can repeat this step over and over until you've fetched as many comments as you need.
Cursor handling example¶
Here's an example of how you can handle the cursor in a loop to fetch comments for a given post.
comments = list()
cursor = 0
ITERATIONS = 10
for _ in range(ITERATIONS):
result = requests.get(
"https://ensembledata.com/apis/tt/post/comments",
params={
"aweme_id": "7411198650782731563",
"cursor": cursor,
"token": "API_TOKEN",
}
).json()["data"]
comments.extend(result["comments"])
cursor = result.get("nextCursor", None)
# If there is no next cursor, we've fetched all the available comments
if cursor is None:
break
print("Number of comments:", len(comments))
comments = list()
cursor = None
ITERATIONS = 10
for _ in range(ITERATIONS):
result = client.tiktok.post_comments(
aweme_id="6849400000000",
cursor=cursor
)
comments.extend(result.data["comments"])
cursor = result.data.get("nextCursor")
# If there is no next cursor, we've fetched all the available comments
if cursor is None:
break
print("Number of comments:", len(comments))
let comments = [];
let cursor;
const ITERATIONS = 10
for (let i = 0; i < ITERATIONS; i++) {
let result = await client.tiktok.postComments({
aweme_id: "6849400000000",
cursor,
})
comments.push(...result.data["comments"])
cursor = result.data["nextCursor"]
// If there is no next cursor, we've fetched all the available comments
if (cursor == undefined) break;
}
console.log("Number of comments:", comments.length)
Output:
Post Comment Replies API¶
Especially on very popular posts TikTok, you'll find that comments can get quite a lot of replies. Scraping replies can help extract more insights from the comment section, so let's see how it's done.
Pretty simple, we just need to specify which comment_id
we want to fetch replies for. We saw where to get the comment id from in a previous section.
The structure of the response is almost identical to that of the Post Comments
API, which we saw in the last section.
Output:
Ok great, but I want those other 36 replies too! Let's fetch them.
Fetching more replies¶
Just like with the Post Comments
API, we can use the nextCursor
to fetch more replies. Let's loop through the replies until we've fetched them all.
replies = list()
cursor = 0
requests_sent = 0
while True:
result = requests.get(
"https://ensembledata.com/apis/tt/post/comments-replies",
params={
"aweme_id": "7411198650782731563",
"comment_id": "7411205662439736097",
"cursor": cursor,
"token": "API_TOKEN",
}
).json()["data"]
replies.extend(result["comments"])
cursor = result.get("nextCursor", None)
requests_sent += 1
# If there is no next cursor, we've fetched all the available replies
if cursor is None:
break
print("Replies:", len(replies))
print("Requests sent:", requests_sent)
replies = list()
cursor = None
requests_sent = 0
while True:
result = client.tiktok.post_comment_replies(
aweme_id="6849400000000",
comment_id="6930120000000",
cursor=cursor
)
replies.extend(result.data["comments"])
cursor = result.data.get("nextCursor")
requests_sent += 1
# If there is no next cursor, we've fetched all the available replies
if cursor is None:
break
print("Replies:", len(replies))
print("Requests sent:", requests_sent)
let replies = [];
let cursor;
let requestsSent = 0
while(true) {
let result = await client.tiktok.postCommentReplies({
awemeId: "6849400000000",
commentId: "6930120000000",
cursor
})
replies.push(...result.data["comments"])
cursor = result.data["nextCursor"]
requestsSent += 1
// If there is no next cursor, we've fetched all the available replies
if (cursor == undefined) break;
}
console.log("Replies:", replies.length)
console.log("Requests sent:", requestsSent)
Output: