drika
drika

Reputation: 1

how to find retweets of each tweet using Tweepy api?

I have been using tweepy to scrape pieces of information from twitter like tweets with a particular keyword, user id, favorite count, retweeters id, etc. I tried to fetch user id who has retweeted certain tweets. After going through tweepy documentation I found the way to do this is.

API.get_retweets(id, *, count, trim_user)

I have gone through similar questions here ,but I could not figure out how to retrieve retweet ids?

number_of_tweets=10
tweets = []
like = []
time = []
author=[]
retweet_count=[]
tweet_id=[]
retweets_list=[]
retweeters_list=[]
for tweet in tweepy.Cursor(api.search_tweets,q='bullying',lang ='en', tweet_mode="extended").items(number_of_tweets):

    tweet_id.append(tweet.id)  # Instead of i._json['id']
    tweets.append(tweet.full_text)
    like.append(tweet.favorite_count)
    time.append(tweet.created_at)
    author.append(tweet.user.screen_name)
    retweet_count.append(tweet.retweet_count)  
    retweets_list = api.get_retweets(tweet.id)
    for retweet in retweets_list:
                retweeters_list.append(retweet.user.id)
#         print(retweet.user.id)

Upvotes: 0

Views: 904

Answers (1)

Mickaël Martinez
Mickaël Martinez

Reputation: 1843

You should chose more explicit names for your variables (i? j?), that would help you (and the helpers here) to check the logic of your code.

Besides, what you want to achieve and what does not work is pretty unclear.

Does it work if you remove the first inner loop? (I don't understand its purpose)

for tweet in tweepy.Cursor(api.search_tweets,q='bullying',lang ='en', tweet_mode="extended").items(number_of_tweets):

    tweet_id.append(tweet.id)  # Instead of i._json['id']

    tweets.append(tweet.full_text)
    like.append(tweet.favorite_count)
    time.append(tweet.created_at)
    author.append(tweet.user.screen_name)
    retweet_count.append(i.retweet_count)

    # No need for the first inner loop

    retweets_list = api.get_retweets(tweet.id)

    for retweet in retweets_list:
        print(retweet.id) # If you want the retweet id
        print(retweet.user.id)

If not, please give more debug information (what is the problem? What do you want to do? Is there any error message? Where? What have you tried? Show some printed variables etc.).

Upvotes: 1

Related Questions