从“user_timeline”中使用tweepy获取完整的推文文本

27

我正在使用 tweepy 通过这里包含的脚本从用户的时间线中获取推文。 但是,推文被截断了:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
new_tweets = api.user_timeline(screen_name = screen_name,count=200, full_text=True)

返回:

Status(contributors=None, 
     truncated=True, 
     text=u"#Hungary's new bill allows the detention of asylum seekers 
          & push backs to #Serbia. We've seen push backs before so\u2026 https:// 
          t.co/iDswEs3qYR", 
          is_quote_status=False, 
          ...

也就是说,对于一些inew_tweets[i].text.encode("utf-8") 看起来像是这样的

#Hungary's new bill allows the detention of asylum seekers & 
push backs to #Serbia. We've seen push backs before so…https://t.co/
iDswEs3qYR

在后者中,...代替了通常会在Twitter上显示的文本。

有人知道我怎样才能覆盖truncated=True从而在我的请求中获取完整的文本吗?


你在做什么来接收那个返回值? - Jonathan Portorreal
抱歉回复慢了,刚看到这个 - 我只是在漂亮地打印 new_tweets[0] - atkat12
可能是Tweepy截断状态的重复问题。 - mountrix
2个回答

39

你需要使用tweet_mode="extended"而非full_text=True。

然后,你应该使用full_text而非text来获取完整的推文文本。

你的代码应该如下所示:

new_tweets = api.user_timeline(screen_name = screen_name,count=200, tweet_mode="extended")

那么为了获取完整的推文文本:

tweets = [[tweet.full_text] for tweet in new_tweets]


2
在我的情况下它没有起作用。我仍然收到被截断的推文。 :( - Verma Aman

13

Manolis的回答不错,但不完整。为了获得推文的扩展版本(如Manolis的版本),你需要执行:

tweetL = api.user_timeline(screen_name='sdrumm', tweet_mode="extended")
tweetL[8].full_text
'Statement of the day at #WholeChildSummit2019 - “‘SOME’ is not a number, and ‘SOON’ is not a time!” IMO, this is why educational systems get stuck. Who in your system will initiate change? TODAY! #HSEFutureReady'
然而,如果这条推文是一条转推,你将需要使用转推的完整文本:
tweetL = api.user_timeline(id=2271808427, tweet_mode="extended")
# This is still truncated
tweetL[6].full_text
'RT @blawson_lcsw: So proud of these amazing @HSESchools students who presented their ideas on how to help their peers manage stress in mean…'
# Use retweeted_status to get the actual full text
tweetL[6].retweeted_status.full_text
'So proud of these amazing @HSESchools students who presented their ideas on how to help their peers manage stress in meaningful ways! Thanks @HSEPrincipal for giving us your time!'

这是使用Python 3.6tweepy-3.6.0进行测试的。


我正好在找这个。谢谢! - Juan C
我该如何确定给定的状态是一条推文还是转发?编辑:不用了,我找到了解决方法。感谢您的回答 :) - Verma Aman
谢谢您发布这篇文章!现在我可以从用户的时间线中获取Twitter用户在推文中的完整文本。 - Zane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接