TypeError: 'int' object is not iterable on Python Twitter API

TypeError: 'int' object is not iterable on Python Twitter API

我正在使用 Twitter 库从推文中提取文本、screen_name、主题标签、关注者数量等。

我可以轻松获取 screen_name、主题标签和文本,因为它们都是字符串。

如何提取 'int' 对象的关注者计数并保存为列表格式?

status_texts = [status['text']
                for status in statuses]
screen_names = [user_mention['screen_name']
                for status in statuses
                    for user_mention in status['entities']['user_mentions']]
followers = [user['followers_count']
            for status in statuses
                for user in status['user']['followers_count']]

前两个代码的结果是

["RT @ESPNStatsInfo: Seven of the NBA's top 10 all-time leading scorers never had back-to-back 50-point games. \n\nKareem Abdul-Jabbar\nKarl Mal…", 'RT @kirkgoldsberry: The game has changed. Rookie LeBron versus Doncic"]
['ESPNStatsInfo', 'kirkgoldsberry', 'ESPNStatsInfo', 'warriors', 'MT_Prxphet', 'Verzilix', 'BleacherReport']

我的预期结果是

[10930,13213,15322,8795,9328,23519]

但是当我尝试提取关注者的数量并将它们保存为列表格式时,它returns TypeError: 'int' object is not iterable。我知道我收到此错误是因为 follower_counts 的结果是整数,我不能将 for 与整数一起使用。

在这种情况下,我需要将 int 转换为 str 吗?还是我需要使用 range ?

我知道使用 tweepy 更简单,但我想先使用 twitter

所以我希望代表您的 API 调用的 json 响应的字典被称为 jsonResponse

这样,您已经知道可以通过 statuses = jsonResponse['statuses'] 获取每条推文。 (我希望你的 statuses 也是如此。)

从那里,我猜你想要每条推文的关注者数量列表。因此,对于状态中的状态,您希望关注者计数。其中,在 python 中看起来像这样:

followers_counts = [status['user']['followers_count'] for status in statuses]

另一种方法是映射 statuses 列表:

followers_count = map(lambda status: status['user']['followers_count'], statuses)

使用 map,您可以更简单地为每条推文制作您想要的信息的字典。但这看起来就像你已经从 API.

中得到的 json