Search code examples
pythontwittertweepy

Random Tweets from past 24 hours using Tweepy


I am trying to extract 100 random tweets from the past 24 hours that contain the hashtag #100DaysOfCode. I'm using tweepy in Python.

I'm not sure if this is possible. There used to be a since parameter you could utilise, but that's no longer available.

 auth = tw.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
 auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
 api = tw.API(auth)
 r = tw.Cursor(api.search_tweets, q = '#100DaysOfCode').items(100)

Is there a simple solution to this?


Solution

  • First of all, you should use the count=100 argument to ask for 100 results per request. items(100) is doing small requests (count=15 per default) until you reach the 100 results.

    Then, a solution could be to request 100 tweets, to check the date of each one and to keep only the ones of the past day, then make a new request and continue until you have your 100 tweets of the past day.

    But the best solution is probably to use the Twitter API v2.

    The /2/tweets/search/recent endpoint has a start_time parameter (see here).

    And you can call it with the search_recent_tweets method in Tweepy (see here)

    [remember that you have to use tweepy.Client and not tweepy.API since it's a V2 endpoint]