【问题标题】:TypeError: 'int' object is not iterable on Python Twitter APITypeError:“int”对象在 Python Twitter API 上不可迭代
【发布时间】:2019-08-18 22:27:55
【问题描述】:

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

获取屏幕名称、主题标签和文本没有问题,因为它们都是字符串。

如何提取“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]

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

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

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

【问题讨论】:

  • 这个错误是不言自明的,status['user']['followers_count'] 返回一个整数,你尝试在其中迭代。此外,您不能将其转换为用户。你必须找到其他方法
  • 所以请告诉我们您期望的结果。也许解决方案是迭代用户以获得他们的followers_count。但是,除非您确切告诉我们您想要什么,否则我们无法知道
  • @Nenri 谢谢你的评论。首先,我知道status['user']['followers_count'] 返回一个整数,但我不知道如何在不使用for 迭代的情况下从推文中提取followers_count。其次,我不明白您所说的“您不能将其转换为用户”是什么意思。推文采用 JSON 格式,followers_countusers 下?
  • 你应该用你真正想要的东西重写你的整个问题。
  • @Nenri 好的,我现在就去做

标签: python for-loop twitter iterable


【解决方案1】:

所以我希望表示来自您的 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。

【讨论】:

  • 谢谢!这正是我想要的。很抱歉用糟糕的英语语法写了一个糟糕的问题。从你的回答来看是statuses = jsonResponse['statuses'] 和这个一样吗? search_results = twitter_api.search.tweets(q=q, count=count)statuses = search_results['statuses']
  • 是的。因为我不知道您使用的是模块还是直接 API 调用,所以我不知道 jsonResponse 是从哪里得到的。但在您的情况下,它与 search_results 相同。
  • 在为每条推文制作关注者数量列表时,您的代码仅使用了一次 for 迭代。但是当我尝试提取 screen_name 就像我在我的问题上写的那样时,我使用了两次 for 迭代。这仅仅是因为我不能为int 使用for 迭代吗?还是 JSON 格式?
  • @김도훈 不,这是因为 user_mentions 是一个列表,因此您从所有推文中检索所有用户提及。我建议您这样做:screen_names = [[user_mention['screen_name'] for user_mention in status['entities']['user_mentions']] for status in statuses] 这将为您提供每条推文的所有 user_mentions 列表,而 user_mentions 是用户提及的所有屏幕名称的列表。 followers_counts 是每个状态的单个值,所以我们只需要一个 for 循环。
  • 好的。很抱歉打扰您,但我对答案代码有最后一个问题。根据您的代码,我能够获得关注者数量列表。为了计算follower_counts的最大数量,我做了print(max(followers_counts)),它返回了20323629。然后我试图通过followers_counts.index(20323629) 知道该号码在列表中的位置,它返回了 926。然后我做了screen_names[926],它返回了“DevinBook”,我在 Twitter 上查看,但他只有 641,885 个关注者。我试过[925],[927],但还是不匹配。这里出了什么问题?
猜你喜欢
  • 2015-04-06
  • 2013-10-31
  • 2016-02-24
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 2023-01-22
相关资源
最近更新 更多