Created
January 10, 2024 23:55
-
-
Save nickjevershed/3b43c84afa8ea967ca060c1a4d2b4677 to your computer and use it in GitHub Desktop.
Demonstration of a script to get TikTok video stats from a hashtag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from TikTokApi import TikTokApi | |
import asyncio | |
import os | |
import json | |
import scraperwiki | |
import asyncstdlib as a | |
ms_token = ['YOUR TOKEN HERE'] | |
async def get_hashtag(hashtag): | |
async with TikTokApi() as api: | |
await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3) | |
tag = api.hashtag(name=hashtag) | |
# user_data = await user.info() | |
# print(user_data) | |
count = 0 | |
async for i, video in a.enumerate(tag.videos(count=200)): | |
# print(video) | |
# jsonDict = json.dumps(video.as_dict, indent=4) | |
print(video.as_dict['desc']) | |
data = {} | |
data['username'] = video.as_dict['author']['uniqueId'] | |
data['title'] = video.as_dict['desc'] | |
data['video_id'] = video.as_dict['id'] | |
data['saved'] = video.as_dict['stats']['collectCount'] | |
data['comments'] = video.as_dict['stats']['commentCount'] | |
data['liked'] = video.as_dict['stats']['diggCount'] | |
data['plays'] = video.as_dict['stats']['playCount'] | |
data['shares'] = video.as_dict['stats']['shareCount'] | |
data['url'] = f"https://www.tiktok.com/@{data['username']}/video/{data['video_id']}" | |
data['order'] = i | |
print(data) | |
scraperwiki.sqlite.save(unique_keys=["video_id"], data=data, table_name=hashtag) | |
# with open("example.json", "w") as outfile: | |
# outfile.write(jsonDict) | |
asyncio.run(get_hashtag("yes23")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment