How to get the latest user status using tweepy

I am trying to use tweepy to get the last user status

My code

api = tweepy.API(auth) for status in tweepy.Cursor(api.user_timeline).items(): lastid = status.id laststatus = api.get_status(lastid).text break 

it works. But I have to use a loop. Is there a better way?

+4
source share
1 answer

.items() returns an iterator, so you can just call next() to get the first element:

 status = next(tweepy.Cursor(api.user_timeline).items()) 

This can throw StopIteration if there are no elements at all. You can add a default value to next() to prevent the following:

 status = next(tweepy.Cursor(api.user_timeline).items(), None) 
+5
source

Source: https://habr.com/ru/post/1469559/


All Articles