Tweepy Twitter get all tweet replies of particular user
10,118
Solution 1
First, find the retweet thread of your conversation with your service provider:
# Find the last tweet
for page in tweepy.Cursor(api.user_timeline, id="253346744").pages(1):
for item in page:
if item.in_reply_to_user_id_str == "151791801":
last_tweet = item
The variable last tweet
will contain their last retweet to you. From there, you can loop back to your original tweet:
# Loop until the original tweet
while True:
print(last_tweet.text)
prev_tweet = api.get_status(last_tweet.in_reply_to_status_id_str)
last_tweet = prev_tweet
if not last_tweet.in_reply_to_status_id_str:
break
It's not pretty, but it gets the job done. Good luck!
Solution 2
user_name = "@nameofuser"
replies = tweepy.Cursor(api.search, q='to:{} filter:replies'.format(user_name)) tweet_mode='extended').items()
while True:
try:
reply = replies.next()
if not hasattr(reply, 'in_reply_to_user_id_str'):
continue
if str(reply.in_reply_to_user_id_str) == "151791801":
logging.info("reply of :{}".format(reply.full_text))
except tweepy.RateLimitError as e:
logging.error("Twitter api rate limit reached".format(e))
time.sleep(60)
continue
except tweepy.TweepError as e:
logging.error("Tweepy error occured:{}".format(e))
break
except StopIteration:
break
except Exception as e:
logger.error("Failed while fetching replies {}".format(e))
break
Related videos on Youtube
Author by
Zul Hazmi
Updated on September 16, 2022Comments
-
Zul Hazmi about 1 year
I am trying to get all replies of this particular user. So this particular user have reply_to_user_id_str of 151791801. I tried to print out all the replies but I'm not sure how. However, I only manage to print out only 1 of the replies. Can anyone help me how to print out all the replies?
My codes are:
for page in tweepy.Cursor(api.user_timeline, id="253346744").pages(1): for item in page: if item.in_reply_to_user_id_str == "151791801": print item.text a = api.get_status(item.in_reply_to_status_id_str) print a.text
-
Zul Hazmi over 8 yearsthanks! It works. Just one question, do you know how you avoid tweet limit? Tried exception but does'nt work
-
jjinking over 8 yearsDo you mean twitter API rate limits? You can set the wait_on_rate_limit parameter to true when you are setting up the api. For example:
api = tweepy.API(auth_args, wait_on_rate_limit=True)
. -
Raunaq Jain about 4 yearsI downvoted it but the answer is actually correct. Would upvote it as soon as it gets unlocked.
-
Malik Faiq almost 4 years@RaunaqJain no problem :)