2

I'm trying to get information about tweets (number of likes, comments, etc) from a specific account between two dates. I have access to the Twitter API and tweepy installed, but I have not been able to figure out how to do this. My authenitcation method is OAuth 2.0 Bearer Token. Any help is appreciated!

Henry Wang
  • 21
  • 1

1 Answers1

2

You can check the time when a tweet was created by looking at the created_at attribute under the tweet object. To fetch all the tweets from a specific user during a specific timeframe, however, you have to first fetch all the tweets under that account. Also, it's worth mentioning that Twitter's API only supports up to 3200 of the user's latest tweets.

To fetch all the tweets, you could do

# Get 200 tweets every time and add it onto the list (200 max tweets per request). Keep looping until there's no more to fetch.
username = ""
tweets = []
fetchedTweets = twitterAPI.user_timeline(screen_name = username, count = 200)
tweets.extend(fetchedTweets)
lastTweetInList = tweets[-1].id - 1

while (len(fetchedTweets) > 0):
        fetchedTweets = twitterAPI.user_timeline(screen_name = username, count = 200, max_id = lastTweetInList)
        tweets.extend(fetchedTweets)
        lastTweetInList = tweets[-1].id - 1
        print(f"Catched {len(tweets)} tweets so far.")

Then, you would have to filter out all the tweets that fall under your specific timeframe (you have to import datetime):

start = datetime.datetime(2020, 1, 1, 0, 0, 0)
end = datetime.datetime(2021, 1, 1, 0, 0, 0)
specificTweets = []
for tweet in tweets:
    if (tweet.created_at > start) and (tweet.created_at < end):
        specificTweets.append(tweet)

You can now see all the tweets that falls under your timeframe in specificTweets.

yangci
  • 21
  • 5
  • Thank you so much! This worked! A follow up question: is there way to get comment count on each status object? I can get favorite_count and retweet_count, but not the number of comments on the tweet. – Henry Wang Jan 13 '21 at 15:44
  • Also, the favorite_count for retweets is zero, is there a good way to get that number as well? – Henry Wang Jan 13 '21 at 15:56
  • It seems like the comment count on each status object isn't available using the API, unless you have the Premium/Enterprise tiered API [(read the docs here under "reply_count")](https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet). There's no direct way to access the reply count, but I found another post showing how to access it from another attribute: [https://stackoverflow.com/questions/2693553/replies-to-a-particular-tweet-twitter-api]. I'm not sure what you mean by the favorite_count for retweets. – yangci Jan 13 '21 at 17:02