如何获得完整的推文文本?

How to get full tweet text?

现在 status.text 行正在获取推文文本,但它被截断了(不是完整的推文文本)。获取推文信息时如何通过on status获取推文全文?

def on_status(self, status):

    if not hasattr(status,'retweeted_status'):

        #db = DatabaseInteractor.DatabaseInteractor()

        text=self.parse_text(status.text)
        created_at=self.parse_text(status.created_at)
        user_id=self.parse_text(status.user.id_str)
        username=self.parse_text(status.user.name)
        location=self.parse_text(status.user.location)
        coordinates=self.parse_text(status.coordinates)
        tweet_id=self.parse_text(status.id_str)
        hashtags=self.parse_text(status.entities['hashtags'])

        print("Created At: " + created_at)
        print("Tweet Text: "  + text)
        print("Tweet ID: " + tweet_id)
        print("Username: " + username)
        print("Username ID: " + user_id)
        print("Location: " + location )
        print("Coordinates: " + coordinates)
        print("Hashtags: " + hashtags)

Taking reference from here

如果你想在 Twitter 上获得全文回复,你需要在调用 API 时添加关键字 tweet_mode='extended',如下所示:

api.search(q='<something to search keyword>', tweet_mode='extended')

通过添加此关键字,您可以在 API 的响应中获得 full_text 字段,而不是文本字段,另请注意,某些推文可能没有扩展文本,因此会出现错误使用 try and except

def on_status(self, status):

    if not hasattr(status,'retweeted_status'):

        #db = DatabaseInteractor.DatabaseInteractor()

        try:    
            text=self.parse_text(status.retweeted_status.extended_tweet['full_text']) #Replace text with extended_tweet['full_text']
        except:
            text=self.parse_text(status.retweeted_status.text)

        created_at=self.parse_text(status.created_at)
        user_id=self.parse_text(status.user.id_str)
        username=self.parse_text(status.user.name)
        location=self.parse_text(status.user.location)
        coordinates=self.parse_text(status.coordinates)
        tweet_id=self.parse_text(status.id_str)
        hashtags=self.parse_text(status.entities['hashtags'])

        print("Created At: " + created_at)
        print("Tweet Text: "  + text)
        print("Tweet ID: " + tweet_id)
        print("Username: " + username)
        print("Username ID: " + user_id)
        print("Location: " + location )
        print("Coordinates: " + coordinates)
        print("Hashtags: " + hashtags)

您似乎在使用 Twitter 流式传输 API(statuses/filter,或 tweepy 中的 StreamListener)。

在这种情况下,如果推文有一个字段表明 truncated: true,您需要在推文对象中查找一个名为 extended_tweet 的附加字段,该字段将包含一个名为 full_text.

上一个答案中建议的 tweet_mode='extended' 参数在 Streaming API 上无效。

在 Twitter 开发者实验室(API 的下一版本目前正在测试中)不再区分截断或扩展的推文,所有推文对象都将 return 全文数据.