Skip to content
Advertisement

tweepy is not giving me the last tweet

Im trying to get the last tweet froom a twitter account using tweepy in python.

I know there are a few similiar answers for example this one Get the last tweet with tweepy

However the issue that I have is that i dont get the last tweet from a persons timeline but the second last tweet.

Im using this code here in a while loop:

JavaScript

If i run this(at time of the writing), i get this tweet from elon:

JavaScript

however looking at his timeline the last tweet from him was this:

JavaScript

So why am i not getting the last tweet? Strangely enough running this script last night it did print out his last tweet. running it now I get the same tweet, it printed out yesterday as the last tweet

and if I run the above code like this (without ‘exclude_replies’)

JavaScript

i get as his last tweet

JavaScript

which was his last reply, so this works.

I just cant fetch the last actual tweet from his timeline

Advertisement

Answer

As Iain Shelvington mentioned in the comments, exclude_replies will also ignore replies to oneself.

I don’t think there’s a direct way of getting a user’s last tweet in their timeline. You could create a function that from the retrieved tweets, gets the first one that:

a) is not a reply, i.e., in_reply_to_screen_name = None.

b) or replies to themselves, i.e., in_reply_to_screen_name = screen_name.

This could look something like:

JavaScript

Then, running:

JavaScript

You get:

JavaScript

This can also be done in a one-liner with:

JavaScript

Note: it should be checked that last_tweet is not None before getting its full_text.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement