The initial method TrendingUsersFullDeatils() works when not in a class and where self. count is substituted for a number.
However, when I created a class and pass through an integer I get an attribute error shown below:
tiktoks = tiktok_api.by_trending(count=self.count)
AttributeError: 'int' object has no attribute 'count'
CODE:
from TikTokApi import TikTokApi
import json
tiktok_api = TikTokApi.get_instance()
tiktok_data = []
class TikTokWebScraper():
def __init__(self, count=0):
self.count = int(count)
def TrendingUsersFullDeatils(self):
tiktoks = tiktok_api.by_trending(count=self.count)
for tiktok in tiktoks:
tiktok_data.append({"Username": tiktok["author"]["uniqueId"],
"Follower Count": tiktok["authorStats"]["followerCount"],
"Id": tiktok["author"]["id"],
"uniqueID": tiktok["author"]["uniqueId"],
"Nickname": tiktok["author"]["nickname"],
"Verfied Status": tiktok["author"]["verified"],
"Private": tiktok["author"]["privateAccount"],
"Following Count": tiktok["authorStats"]["followingCount"],
"Follower Count": tiktok["authorStats"]["followerCount"],
"Heart Count": tiktok["authorStats"]["heartCount"],
"Video Count": tiktok["authorStats"]["videoCount"]})
f = open("tiktok.json", "w")
j = json.dumps(tiktok_data, indent= 4)
f.write(j)
f.close()
TikTokWebScraper.TrendingUsersFullDeatils(10)
What amendments do I need to make to the class in order to pass through an integer to the method, as I have looked online and the solutions don’t match mine for some reason.
The following code will not function when imported into code as the necessary modules need to be installed and retrieved from a git repository.
>Solution :
You haven’t initialized your object (haven’t called your init method). Also you’re passing 10 as argument to TrendingUsersFullDeatils method, where the only argument is self. That’s why it tries to get ‘count’ field in integer. Try this:
TikTokWebScraper(10).TrendingUsersFullDeatils()