类型错误:';int';对象在Python Twitter API上不可编辑

类型错误:';int';对象在Python Twitter API上不可编辑,python,for-loop,twitter,iterable,Python,For Loop,Twitter,Iterable,我正在使用Twitter库从推文中提取文本、屏幕名称、标签、关注者数量等 我在获取屏幕名称、hashtag和文本方面没有问题,因为它们都是字符串 如何提取“int”对象的跟随者计数并保存为列表格式 status_texts = [status['text'] for status in statuses] 前两个代码的结果如下所示 ["RT @ESPNStatsInfo: Seven of the NBA's top 10 all-time leading sc

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

我在获取屏幕名称、hashtag和文本方面没有问题,因为它们都是字符串

如何提取“int”对象的跟随者计数并保存为列表格式

status_texts = [status['text']
                for status in statuses]
前两个代码的结果如下所示

["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"]
我的预期结果是

[10930123153228795932823519]

但是,当我试图提取追随者的数量并将其保存到列表格式时,它返回
TypeError:“int”对象不可编辑
。我知道我得到了这个错误,因为follower_计数的结果是整数,我不能将
用于带整数的

在这种情况下,是否需要将
int
转换为
str
?或者我需要使用
范围


我知道使用
tweepy
要简单得多,但我想先使用
twitter
,所以我希望从API调用中调用表示json响应的词汇表
jsonResponse

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

从那里,我猜你想要一个列表,列出每条推文的关注者数量。因此,对于status in status,您需要追随者计数。在python中,它看起来就像这样:

followers\u counts=[状态['user']['followers\u count']表示状态为状态]
另一种方法是映射
状态
列表:

followers\u count=map(lambda状态:状态['user']['followers\u count'],状态)

使用
map
你甚至可以更简单地为每条tweet创建你想要的信息的词汇表。但是,这看起来就像您已经从API获得的json。

错误几乎是不言自明的,
status['user']['followers\u count']
返回一个整数,您尝试在其中迭代。此外,您不能将其转换为用户。你得另找一条路,告诉我们你期望的结果。也许解决方案是对用户进行迭代以获得其追随者数量。但在你告诉我们你到底想做什么之前我们无法知道want@Nenri谢谢你的评论。首先,我知道
status['user']['followers\u count']
返回一个整数,但我不知道如何从tweet中提取
followers\u count
,而不使用
进行迭代。其次,我不明白你所说的“你不能把它转换成用户”是什么意思。Tweets是JSON格式的,
followers\u count
users
下?你应该用你真正想要的内容重写你的整个问题。@Nenri好的,我现在就做谢谢!这正是我想要的。对不起,这个问题用蹩脚的英语语法写。您的答案是
statuses=jsonResponse['statuses']
与此相同吗
search\u results=twitter\u api.search.tweets(q=q,count=count)
statuses=search\u results['statuses']
是的。因为我不知道您使用的是模块还是直接API调用,所以我不知道从中得到了什么
jsonResponse
。但这与您的案例中的
搜索结果
相同。当为每条推文列出关注者数量时,您的代码只对
迭代使用了一次
。但当我试图提取屏幕名称时,就像我在问题上写的那样,我使用了两次
迭代。这仅仅是因为我不能对
int
使用
迭代吗?还是JSON的格式@김도훈 不,这是因为用户提到是一个列表,所以您可以从所有推文中检索所有用户提到的内容。我建议您这样做:
screen\u names=[[user\u-title['screen\u-name']对于状态中的用户提及['entities']['user\u-titles']]对于状态中的用户提及]
这将为您提供每个tweet的所有用户提及的列表,而
user\u-titles
是用户提及的所有屏幕名称的列表
followers\u counts
是每个状态的一个值,所以我们只需要一个值来表示loop.Ok。很抱歉打扰你,但我还有最后一个关于答案代码的问题。根据你的代码,我可以得到一个追随者数量列表。为了计算跟随者计数的最大数量,我做了
print(max(followers\u counts))
,它返回了
20323629
。然后我试图通过
followers\u counts.index(20323629)
知道这个数字在列表中的位置,它返回926。然后我做了
screen\u names[926]
,返回了“DevinBook”,我在twitter上查看了一下,但他只有641885个追随者。我尝试了
[925],[927]
,但仍然不匹配。这里出了什么问题?
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']