skip to Main Content

Novice programmer here seeking help.

I already set up my code to my requirements to use the Twitter’s premium API.

SEARCH_TERM = '#AAPL OR #FB OR #KO OR #ABT OR #PEPCO'
PRODUCT = 'fullarchive'
LABEL = 'my_label'
r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), 
                {'query':SEARCH_TERM, 'fromDate':201501010000, 'toDate':201812310000})

However, when I run it I obtain the maximum number of tweets per search which is 500.
My question is should I add to the query maxResults = 500? And how do I use the next parameter to keep the code running until all the tweets that correspond to my query are obtained?

2

Answers


  1. Use the count variable in a raw_query, for example:

    results = api.GetSearch(
        raw_query="q=twitter%20&result_type=recent&since=2014-07-19&count=100")
    
    Login or Signup to reply.
  2. To up the results from the default of 100 to 500, yes, add maxResults to the query like this:

    r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL), 
        {
            'query':SEARCH_TERM, 
            'fromDate':201501010000, 'toDate':201812310000,
            'maxResults':500
        })
    

    You can make successive queries to get more results by using the next parameter. But, even easier, you can let TwitterAPI do this for you by using the TwitterPager class. Here is an example:

    from TwitterAPI import TwitterAPI, TwitterPager
    SEARCH_TERM = '#AAPL OR #FB OR #KO OR #ABT OR #PEPCO'
    PRODUCT = 'fullarchive'
    LABEL = 'my_label'
    api = TwitterAPI(<consumer key>, 
                     <consumer secret>,
                     <access token key>,
                     <access token secret>)
    pager = TwitterPager(api, 'tweets/search/%s/:%s' % (PRODUCT, LABEL), 
        {
            'query':SEARCH_TERM, 
            'fromDate':201501010000, 'toDate':201812310000
        })
    for item in pager.get_iterator():
        print(item['text'] if 'text' in item else item)
    

    This example will keep making successive requests with the next parameter until no tweets can be downloaded.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search