Search code examples
pythondatetimetwittertweepy

Retrieving tweets from since the start of the month with tweepy


I'm trying to retrieve tweets from the start of the current month using Tweepy. Here is how I defined the dates:

now = datetime.now()
monthstart = now.replace (day = 1)

and how I try to retrieve tweets:

#test print
a = [] #raw
b = [] #preprocessed
c = [] #place
d = [] #coord
for tweet in tweepy.Cursor(api.search,q="#lalinbdg", tweet_mode = 'extended',
                           since=monthstart).items():
                           a.append(tweet.full_text)
                           b.append(tweet.full_text)
                           c.append(tweet.place)
                           d.append(tweet.coordinates)
                           b = list(dict.fromkeys(b)) #remove duplicates
                           for text in b:
                             text = text.casefold() #lowercase
                             print(text)

When I try to specify the date with the monthstart variable it won't return any output. It seems like the 'since' only accepts date strings since it works when I use one like '2020-07-01', does anyone have a solution for this?


Solution

  • The since parameter needs to be a string, and you are passing a datetime object. So you need to convert it to a string using strftime. Note that datetime.now() has a time component, so you would be better using date.today() otherwise you might miss some entries from times on the 1st of the month which are earlier than the time now. In all:

    today = date.today()
    monthstart = today.replace(day=1).strftime('%Y-%m-%d')