Search code examples
pythontwitterpython-twitter

How do I know how many result pages can I get using python-twitter GetSearch() method


I use twitter-python search API to retrieve search results like below

import twitter
api = twitter.Api()
i = 1
result = api.GetSearch("Avatar", page=i)
print [s.text for s in result]

The code above means I want to get the result on first page returned. I tried multiple assignment of i they all work. But I don't know what is the maxium value of i can I assign. Any idea?


Solution

  • The maximum value is going to depend on the query, and how many pages twitter feels like giving you.

    What about using try/except?

    import twitter
    api = twitter.Api()
    
    # try a large page number:
    i = 181
    
    try:
        result = api.GetSearch("Avatar", page=i)
        print [s.text for s in result]
    except:
        print 'page is out of range'