我正在尝试使用Tweepy的用户的最后一条推文。这是我的代码:
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'
我理解错误,但是如何从状态中获取文本? Tweepy的文档很糟糕且不完整......
非常感谢你的帮助,
祝你今天愉快 ! :)
答案 0 :(得分:6)
API {.user_timelines的返回值,如API reference中所述,是状态对象的列表。
def get_last_tweet(self):
tweet = self.client.user_timeline(id = self.client_id, count = 1)[0]
print(tweet.text)
请注意额外的[0]
来获取第一个条目。
我非常确定Tweepy的作者乐意接受带有改进文档的拉取请求;)