I am a python newbie. I am learning how to use api.

I want to use TwitterAPI to get tweets including "coronavirus", but I want to know the number of those tweets. It seems that there is a limit on the number of requests and I can not get all the tweets, but specify a time between 2020-7-13_00:00:00 and 2020-7-13_00:01:00, and from there I want to calculate the number of cases, but I do not know how to specify the time.

import urllib
from requests_oauthlib import OAuth1
import requests
import sys

def main():
    # API private key
    CK = `` # Consumer key
    CKS = `` # Consumer Secret
    AT ='' # access token
    ATS = `` # access token secret
    # Search parameters
    word ='coronavirus' # search word
    count = 100 # number of searches per time (maximum 100/default is 15)
    range = 5 # Upper limit of number of searches (up to 180/15 minutes reset)
   # Tweet search/text extraction
    tweets = search_tweets(CK, CKS, AT, ATS, word, count, range)
    #Display search results

def search_tweets(CK, CKS, AT, ATS, word, count, range):
    # Set string
    word +='exclude:retweets' # except RT
    word = urllib.parse.quote_plus(word)
    # request
    url = "https://api.twitter.com/1.1/search/tweets.json?lang=ja&q="+word+"&count="+str(count)
    auth = OAuth1(CK, CKS, AT, ATS)
    response = requests.get(url, auth=auth)
    data = response.json()['statuses']
    # Second and subsequent requests
    cnt = 0
    tweets = []
    while True:
        if len(data) == 0:
        cnt += 1
        if cnt>range:
        for tweet in data:
            maxid = int(tweet["id"])-1
        url = "https://api.twitter.com/1.1/search/tweets.json?lang=ja&q="+word+"&count="+str(count)+"&max_id="+str(maxid)
        response = requests.get(url, auth=auth)
            data = response.json()['statuses']
        except KeyError: # Data error handling when the number of requests reaches the upper limit
            print('Searched up to limit')
    return tweets

if __name__ =='__main__':

I use the code like this.

Is there any way? Thank you.

  • Answer # 1

    According to the answer from tweepy get tweets between two datesAPIIt seems that you can not specify the time.
    According to Tweet objectscreated_atSince the date and time of tweet is stored in, you need to filter by yourself based on it.
    For concrete code, I think it might be useful to refer to the python script example that narrows down the unixtimestamp criteria based on the created_at time obtained with the Twitter Timeline API.