如何找到包含 json 数据的推文的全文(在 tweepy 中)?

How do I find the full text of a tweet with json data (in tweepy)?

在 tweepy 中,我正在尝试流式传输数据,但我只能在屏幕上显示少量文本。我花了 30 分钟试图找到答案,但 none 成功了。我只是想获取完整的推文文本,谢谢

import tweepy
import json
from time import sleep
import requests

# Specify the account credentials in the following variables:
consumer_key = 'xxxxxxxxxx'
consumer_secret = 'xxxxxxxxxx'
access_token = 'xxxxxxxx-xxxxxxxxx'
access_token_secret = 'xxxxxxxxxx'

# This listener will print out all Tweets it receives
class PrintListener(tweepy.StreamListener):
    def on_data(self, data):
        # Decode the JSON data
        tweet = json.loads(data)

        id = tweet["id"]
        user = tweet['user']['screen_name']
        user_message = tweet['text']

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    listener = PrintListener()

    # Show system message
    print('searching stuff ==>')

    # Authenticate
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)
    # Connect the stream to our listener
    stream = tweepy.Stream(auth, listener)
    # stream.filter(track=['test'])
    stream.filter(track=['test'])

如果不是转推,您可以使用代表推文的 JSON 数据的 extended_tweet 字段和该字段的 full_text 子字段来获取全文的推文。否则,如果它是转推,则必须使用表示转推推文的 JSON 数据执行此操作,这将是转推 JSON 数据的 retweeted_status 字段。请注意,extended_tweet 字段仅适用于扩展推文。

我建议使用传递给 on_status 的 Status 对象和该对象的相应属性,因为您似乎没有做任何需要原始 JSON 数据的事情。

有关扩展推文的更多信息,请参阅Tweepy's documentation on the subject and Twitter's Tweet updates documentation