【发布时间】: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_count在users下? -
你应该用你真正想要的东西重写你的整个问题。
-
@Nenri 好的,我现在就去做
标签: python for-loop twitter iterable