skip to Main Content

I’m looking into the Twitter Search API, and apparently, it has a count parameter that determines “The number of tweets to return per page, up to a maximum of 100.” What does “per page” mean, if I’m for example running a python script like this:

import twitter #python-twitter package
api = twitter.Api(consumer_key="mykey", 
                consumer_secret="mysecret", 
                access_token_key="myaccess", 
                access_token_secret="myaccesssecret")
results = api.GetSearch(raw_query="q=%23myHashtag&geocode=59.347937,18.072433,5km")
print(len(results))

This will only give me 15 tweets in results. I want more, preferably all tweets, if possible. So what should I do? Is there a “next page” option? Can’t I just specify the search query in a way that gives me all tweets at once? Or if the number of tweets is too large, some maximum number of tweets?

2

Answers


  1. Tweepy has a Cursor object that works like this:

    for tweet in tweepy.Cursor(api.search, q="#myHashtag&geocode=59.347937,18.072433,5km", lang='en', tweet_mode='extended').items():
        # handle tweets here
    

    You can find more info in the Tweepy Cursor docs.

    Login or Signup to reply.
  2. With TwitterAPI you would access pages this way:

    pager = TwitterPager(api, 
                         'search/tweets', 
                         {'q':'#myHashtag', 'geocode':'59.347937,18.072433,5km'})
    for item in pager.get_iterator():
        print(item['text'] if 'text' in item else item)
    

    A complete example is here: https://github.com/geduldig/TwitterAPI/blob/master/examples/page_tweets.py

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