Search code examples
pythoncsvtwittertweepy

What is the attribute in tweepy.Cursor to print tweets before a certain time?


I have specified to extract tweets since a specified date but I also need to extract tweets before a specified date. The since keyword is used to extract tweets since the given date. SO there must be a keyword which extracts tweets before the specified date. What is that keyword and how to use it?

    import tweepy

    import csv

    import pandas as pd

    ####input your credentials here

    consumer_key = ''

    consumer_secret = ''

    access_token = ''

    access_token_secret = ''

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth,wait_on_rate_limit=True)

    csvFile = open('demon4.csv', 'a')

    csvWriter = csv.writer(csvFile)

    for tweet in tweepy.Cursor(api.search,q="#unitedAIRLINES",count=100,lang="en",\

                               since="2017-04-03").items():

        print (tweet.created_at, tweet.text)

        csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])

Solution

  • In the "q" parameter you can use "since" and "until" like this :

    q="#unitedAIRLINES since:2017-04-02 until:2017-04-03"
    

    The result shoud be the same as this advanced search on the official web site :

    https://twitter.com/search?f=tweets&vertical=default&q=%23unitedAIRLINES%20since%3A2017-04-02%20until%3A2017-04-03&src=typd

    Except that with the public search API you just can get 7 days past.

    You can either use a specific tweet id as a starting point. The parameter is "since_id". And a "max_id" to delimit the period. For more information, see : https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html