现在,行status.text正在获取tweet文本,但它被截断了(不是完整的tweet文本)。获取tweet相关信息时,如何使用on status获取tweet全文?
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)
发布于 2020-06-16 12:50:39
看起来您使用的是Twitter Streaming (statuses/filter
,或者tweepy中的StreamListener
)。
在这种情况下,如果Tweet有一个表示truncated: true
的字段,则需要在名为extended_tweet
的Tweet对象中查找另一个字段,该对象将包含一个名为full_text
的字段。
上一个答案中建议的tweet_mode='extended'
参数在流式接口上无效。
在Twitter开发人员实验室(目前正在测试的API的下一个版本)中,截断或扩展的Tweet之间不再有区别,所有的Tweet对象都将返回全文数据。
发布于 2020-06-16 02:37:01
如果您想获取Twitter响应的全文,则需要在调用如下接口时添加关键字tweet_mode='extended‘:
api.search(q='<something to search keyword>', tweet_mode='extended')
通过添加此关键字,您可以从接口的响应中获取full_text字段,而不是文本字段,同时请注意,某些tweet可能没有扩展文本,因此会给出错误,因此请使用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)
https://stackoverflow.com/questions/62400032
复制相似问题