Tweepy输出有问题

问题描述 投票:0回答:1

我正在尝试处理Twitter用户时间轴上的Tweepy输出。我尝试将输出视为JSON,但无济于事。下面是我如何检索数据,以及使用数据的一些尝试以及随后收到的错误。

我如何检索数据:

user_timeline = api.user_timeline(screen_name='TechCrunch', count=5)

部分输出:

[Status(_api=, _json={'created_at': 'Tue Mar 24 18:02:08 +0000 2020', 'id': 1242512035159687169, 'id_str': '1242512035159687169', 'text': 'RT @Yair_Rosenberg: Please listen to...'''

尝试输入的代码:

Timeline_result = json.loads(user_timeline)

错误消息:

TypeError: the JSON object must be str, bytes or bytearray, not ResultSet

尝试输入的代码:

Timeline_result = json.dumps(user_timeline)

错误消息:

TypeError: Object of type Status is not JSON serializable

我能够从该数据集中提取任何可读内容的唯一方法是使用以下代码-

user_timeline[0].text

部分输出:

'RT @Yair_Rosenberg: Please listen to Dr...'

最终,我希望能够分别提取所有相关信息,即'id','id_str','text'等...

谢谢大家的帮助。我已经将头撞在墙上的墙上了一段时间...

python json tweepy
1个回答
1
投票

Tweepy的user_timeline返回类型为Status的对象的列表。

为了访问单独的idid_strtext,我们可以像user_timeline对象那样迭代:

user_timeline = api.user_timeline(screen_name='TechCrunch', count=5) 
for tweet in user_timeline:
    tweet._json['id_str']

由于每个状态都有一个tweet json,因此您可以找到其属性here。另外,here是实际的实现。

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.