【发布时间】:2022-01-05 23:02:23
【问题描述】:
初始方法 TrendingUsersFullDeatils() 在不在类中且 self. count 替换为一个数字。 但是,当我创建一个类并传递一个整数时,出现如下所示的属性错误:
tiktoks = tiktok_api.by_trending(count=self.count)
AttributeError: 'int' object has no attribute 'count'
代码:
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)
我需要对类进行哪些修改才能将整数传递给方法,因为我在网上查看过,由于某种原因,解决方案与我的不匹配。
以下代码在导入代码时将不起作用,因为需要安装必要的模块并从 git 存储库中检索。
【问题讨论】:
-
看来您必须在某处分配了一个名称为
self的 int。 -
将计数传递给 init,您的方法不接受除 self 以外的参数:
TikTokWebScraper(10).TrendingUsersFullDeatils() -
@Lesiak 谢谢!!!!我正在将一个参数传递给一个不接受它的方法。
标签: python oop attributeerror