【问题标题】:AttributeError: 'int' object has no attribute 'count' [closed]AttributeError:'int'对象没有属性'count'[关闭]
【发布时间】: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


【解决方案1】:

您尚未初始化您的对象(尚未调用您的 init 方法)。此外,您将 10 作为参数传递给 TrendingUsersFullDetails 方法,其中唯一的参数是 self。这就是为什么它试图以整数形式获取“计数”字段。试试这个:

TikTokWebScraper(10).TrendingUsersFullDeatils()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2015-09-24
    • 1970-01-01
    • 2020-05-31
    • 2021-11-30
    • 2020-03-26
    • 2013-04-15
    • 2021-01-18
    相关资源
    最近更新 更多