使用 tweepy 获取最后一条推文 [英] Get the last tweet with tweepy
本文介绍了使用 tweepy 获取最后一条推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Tweepy 获取用户的最后一条推文.这是我的代码:
I'm trying to have the last tweet of the user with Tweepy. This is my code :
class Bot:
def __init__(self, keys):
self._consumer_key = keys[0]
self._consumer_secret = keys[1]
self._access_token = keys[2]
self._access_secret = keys[3]
try:
auth = tweepy.OAuthHandler(self._consumer_key,
self._consumer_secret)
auth.set_access_token(self._access_token, self._access_secret)
self.client = tweepy.API(auth)
if not self.client.verify_credentials():
raise tweepy.TweepError
except tweepy.TweepError as e:
print('ERROR : connection failed. Check your OAuth keys.')
else:
print('Connected as @{}, you can start to tweet !'.format(self.client.me().screen_name))
self.client_id = self.client.me().id
def get_last_tweet(self):
tweet = self.client.user_timeline(id = self.client_id, count = 1)
print(tweet.text)
# AttributeError: 'ResultSet' object has no attribute 'text' .
我理解错误,但如何从状态中获取文本?
I understand the error, but how can I have the text from a status?
推荐答案
API.user_timelines 的返回值,如 API 参考,是状态对象的列表.
The return value of API.user_timelines, as stated in the API reference, is a list of Status objects.
def get_last_tweet(self):
tweet = self.client.user_timeline(id = self.client_id, count = 1)[0]
print(tweet.text)
注意额外的 [0]
以获取第一个条目.
Note the additional [0]
to fetch the first entry.
我很确定 Tweepy 的作者很乐意接受改进文档的拉取请求 ;)
I'm pretty sure the author of Tweepy happily accepts a pull request with improved documentation ;)
这篇关于使用 tweepy 获取最后一条推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文