Reputation: 21
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!
Upvotes: 2
Views: 1095
Reputation: 21
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
.
Upvotes: 2