首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:'int‘对象没有属性'count’

AttributeError:'int‘对象没有属性'count’
EN

Stack Overflow用户
提问于 2022-01-05 23:02:23
回答 1查看 1.1K关注 0票数 -1

初始方法TrendingUsersFullDeatils()不是在类中工作,而是在何处运行。计数被替换为数字。然而,当我创建一个类并传递一个整数时,我会得到一个属性错误,如下所示:

代码语言:javascript
复制
tiktoks = tiktok_api.by_trending(count=self.count)
AttributeError: 'int' object has no attribute 'count'

代码:

代码语言:javascript
复制
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存储库中检索。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-05 23:14:02

您还没有初始化对象(尚未调用init方法)。另外,您要将10作为参数传递给TrendingUsersFullDeatils方法,其中唯一的参数是self。这就是为什么它试图在整数中获得“计数”字段的原因。试试这个:

代码语言:javascript
复制
TikTokWebScraper(10).TrendingUsersFullDeatils()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70600613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档