【问题标题】:How to get full tweet text?如何获取完整的推文文本?
【发布时间】:2020-10-05 13:29:31
【问题描述】:

现在行 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)

【问题讨论】:

    标签: json python-3.x twitter tweepy


    【解决方案1】:

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

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

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

    在 Twitter 开发人员实验室(当前正在测试的 API 的下一版本)中,不再有截断或扩展推文之间的区别,所有推文对象都将返回全文数据。

    【讨论】:

      【解决方案2】:

      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)
      

      【讨论】:

        猜你喜欢
        • 2017-03-29
        • 1970-01-01
        • 2020-10-09
        • 2017-07-31
        • 2022-07-04
        • 2018-12-16
        • 2019-05-12
        • 1970-01-01
        • 2018-05-03
        相关资源
        最近更新 更多